package chi.OOP.demo04;
public class Application {
public static void main(String[] args) {
//Object->Person->Teacher
//Object->Person->Student
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("==========================");
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
// System.out.println(object instanceof String);编译出错
System.out.println("==========================");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);编译出错
// System.out.println(object instanceof String);编译出错
System.out.println("==========================");
Person person1 = new Person();
System.out.println(person1 instanceof Student);//false
System.out.println(person1 instanceof Person);//true
System.out.println(person1 instanceof Object);//true
System.out.println(person1 instanceof Teacher);//false
//System.out.println(person1 instanceof String);编译出错
}
}
instanceof方法
最新推荐文章于 2023-10-04 21:24:51 发布