instanceof (类型转换) 引用类型,判断一个对象是什么类型,或这是否存在父子关系。
语法 x instance y
代码测试
package opp.Demo03;
public class Person {
}
package opp.Demo03;
public class Student extends Person{
}
package opp.Demo03;
public class Teacher {
}
package opp.Demo03;
public class Application {
public static void main(String[] args) {
//Object > Person > Student
Object object = new Student();
System.out.println(object instanceof Object);
System.out.println(object instanceof Person);
System.out.println(object instanceof Student);
System.out.println(object instanceof String);
System.out.println(object instanceof Teacher);
}
}