class1.isAssignableFrom(class2) :class2是不是class1的子类或者子接口
o instanceof TypeName:用来判断一个对象实例 o 是否是一个类或接口(TypeName)的或其子类子接口的实例
public class Test { public static void main(String[] args) { People it=new IT(); System.out.println(People.class.isAssignableFrom(it.getClass())); System.out.println(Object.class.isAssignableFrom(it.getClass())); } } interface People { void eat(); } class IT implements People{ @Override public void eat() { } }
结果:
true true