instanceof: 引用类型,判断一个对象是什么类型
实例:
分别创建3个类为Cat、Dog、Animal:
public class Cat extends Animal{
}
public class Dog extends Animal{
}
public class Animal {
}
在主方法中进行测试:
public class Test {
public static void main(String[] args) {
//Object > Animal >Cat
Object object = new Cat();
System.out.println(object instanceof Cat);
System.out.println(object instanceof Animal);
System.out.println(object instanceof Dog);
}
}
测试结果:
true
true
false
Process finished with exit code 0
x instanceof y ==true
那么x和y之间必须有联系