十一、instanceof和类型转换

public class Demo02 {


    public static void main(String[] args) {
        //Object>Person>Student
        //Object>Person>Teacher
        //Object>String
        Object object = new Student();
        System.out.println(object instanceof Student);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Object);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);

        System.out.println("=======================");
        Person person = new Student();
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Teacher);
        //System.out.println(person instanceof String);//编译报错

        System.out.println("=======================");

        Student student = new Student();
        System.out.println(student instanceof Student);
        System.out.println(student instanceof Person);
        System.out.println(student instanceof Object);

        //System.out.println(student instanceof Teacher);//编译报错
        //System.out.println(person instanceof String);//编译报错
    }
}

 

posted @ 2022-05-13 02:16  Epiphany8Z  阅读(16)  评论(0编辑  收藏  举报