instanceof与类型转换
直接看例子 细品。
import Notto5.Person;
import Notto5.Student;
import Notto5.Teacher;
public class Application{
public static void main(String[] args) {
//Object>Person>Student
//Object>Person>Teacher
//Object>String
//instanceof 用于判断是否存在父子关系 返回boolean类型
Object object = new Student();
//左侧右侧同时相关时返回true
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("========================");
Student student = new Student();
System.out.println(student instanceof Student);//true
//System.out.println(student instanceof Teacher);//编译错误
}
}