instanceof 它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。
class Parent { public String name; } class Child extends Parent { public int age; } public class Test { public static void main(String[] args) { Child c = new Child(); System.out.println("c instanceof Child:"+ (c instanceof Child)); System.out.println("c instanceof Parent:"+ (c instanceof Parent)); } } 结果: c instanceof Child:true c instanceof Parent:true