java学习笔记45--instanceof和类型转换

例如:
    public class Person{
        public void run(){}
    }
    public class Student extends Person{
    }
    public class Teacher extends Person{
    }
    
    例如:
        main:
            Object o = new Student();
            System.out.println(o instanceof Student);//true
            System.out.println(o instanceof Person);//true
            System.out.println(o instanceof Object);//true
            System.out.println(o instanceof Teacher);//false
            System.out.println(o instanceof String);//false

            ---------------------------
            
            Person o = new Student();
            System.out.println(o instanceof Student);//true
            System.out.println(o instanceof Person);//true
            System.out.println(o instanceof Object);//true
            System.out.println(o instanceof Teacher);//false
            //编译报错
            System.out.println(o instanceof String);

            
            ---------------------------

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


            注1:
                System.out.println(x instanceof Y);
                该代码能否编译通过,主要是看声明变量x的类型和Y是否存在子父类的关系.有子父类关系就编译通过,没有子父类关系就是编译报错
            
            注2:
                System.out.println(x instanceof Y);
                输出结果是true还是false,主要是看变量x所指向的对象实际类型是不是Y类型的子类型.
    
    例如:
        main:
            Object o = new Person();
            System.out.println(o instanceof Student);//false
            System.out.println(o instanceof Person);//true
            System.out.println(o instanceof Object);//true
            System.out.println(o instanceof Teacher);//false
            System.out.println(o instanceof String);//false

    

    类型转换
        public class Person{
            public void run(){}
        }
        public class Student extends Person{
            public void go(){}
        }
        public class Teacher extends Person{
        }

        1)为什么要类型转换
            //编译报错,因为p声明的类型Person中没有go方法
            Person p = new Student();
            p.go();


            //需要把变量p的类型进行转换
            Person  p = new Student();
            Student s = (Student)p;
            s.go();
            或者
            //注意这种形式前面必须要俩个小括号
            ((Student)p).go();            
            

        2)类型转换中的问题
            //编译通过
            Object o = new Student();
            Person p = (Person)o;
        
            //编译通过
            Object o = new Student();
            Student s = (Student)o;

            
            //编译通过,运行报错
            Object o = new Teacher();
            Student s = (Student)o;

            
            即:
                X x = (X)o;
                运行是否报错,主要是变量o所指向的对象实现类型,是不是X类型的子类型,如果不是则运行就会报错。

猜你喜欢

转载自blog.csdn.net/onepiece_loves/article/details/88632759