怎么样判断某个类是否是另一个类的子类

解决方案 »

  1.   

    先判断instanceof
    在比较类名,如果不同就是子类
      

  2.   

    /*
     * Created on 2004-9-6
     *
     * To change the template for this generated file go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    package mde.db;import java.util.Calendar;/**
     * @author Administrator
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
    */class Base {
    private int a = 0;
    }class Child extends Base {
    private int b = 0;

    }public class Test {

    public static void main(String[] args) {

    Child c = new Child();


    if (c instanceof Base) {
    System.out.println("OK");
    }






    }}
      

  3.   

    A a = new A();
    if( a instanceof B){
       //A是B的子类
    }else{
       //A不是B的子类
    }
      

  4.   

    楼上说的办法都不行啊,instanceof只能判断是否是相同的类,不能判断是否是父子关系。
    schwarzenegger(找不到服务器) 说“类名不同就是子类”也不行啊,因为有可能两个类毫无关系,或者是“兄弟类”。
      

  5.   

    先判断instanceof
    在比较类名,如果不同就是子类
    ------------------------------
    是对的,先判断有没有继承关系instanceof ;是的话再排除是类本身,在比较类名不时地话就是子类!
      

  6.   

    //Class Parent;
    Class Child;Parent p= ...;  // not null
    Object o = ...; // not null
    if(o.getClass().getName().indexOf(p.getClass().getName()) == 0) {
       // o is subclass of Parent
      

  7.   

    做了一个项目服务网:http://www.cpsol.net/  cpsol的意思是china project service online 即中国项目服务在线,欢迎大家提点意见,感激不尽!!!:)