/* * 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"); }
}}
A a = new A(); if( a instanceof B){ //A是B的子类 }else{ //A不是B的子类 }
//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
做了一个项目服务网:http://www.cpsol.net/ cpsol的意思是china project service online 即中国项目服务在线,欢迎大家提点意见,感激不尽!!!:)
在比较类名,如果不同就是子类
* 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");
}
}}
if( a instanceof B){
//A是B的子类
}else{
//A不是B的子类
}
schwarzenegger(找不到服务器) 说“类名不同就是子类”也不行啊,因为有可能两个类毫无关系,或者是“兄弟类”。
在比较类名,如果不同就是子类
------------------------------
是对的,先判断有没有继承关系instanceof ;是的话再排除是类本身,在比较类名不时地话就是子类!
Class Child;Parent p= ...; // not null
Object o = ...; // not null
if(o.getClass().getName().indexOf(p.getClass().getName()) == 0) {
// o is subclass of Parent