package com.oop.demo02;
public class Aplication {
public static void main(String[] args) {
//instanceof是判断两者之间是否存在继承关系,也就是说是否存在父子继承关系
//Object > String
//Object > Person > Student
//Object > Person > Teacher
Object obj = new Student();
System.out.println(obj instanceof Student);//true
System.out.println(obj instanceof Person);//true
System.out.println(obj instanceof Object);//true
System.out.println(obj instanceof Teacher);//false
System.out.println(obj instanceof String);//true
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(person 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(student instanceof String);//编译失败
}
}
instanceof的说明
instanceof是判断两者之间是否存在继承关系,也就是说是否存在父子继承关系
函数关系式: X instanceof Y (也就是判断X与Y之间是否存在继承关系)