instanceof 和 类型转换

instanceof 和 类型转换

public class Person {
    public void run(){
        System.out.println("Person run");
    }

}


/*
    //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型不确定:父类的引用指向子类
        // Student 能调用的方法都是自己的或者从父类继承过来的
        Student s1 = new Student();
        //父类可以指向子类,但是不能调用独属于子类的方法
        Person s2 = new Student();
        // 根目录类
        Object s3 = new Student();

        //对象能执行那些方法,主要看对象左边的类型,和右边关系不大
        s1.run();
        s2.run();//子类重写的父类的方法,执行子类方法

        s1.eat();
        // ((Student) s2).eat(); 强制类型转换 高(父) ——> 低(子)
        // s2.eat; 无法执行
 */

/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,需要有继承关系,否则会出现 类型转换异常 ClassCastException!
3.存在条件:有继承关系;方法需要重写 ; 父类引用指向子类对象! father f1 = new son();
PS:以下情况不能被重写
    ① static 修饰的方法 ,属于类,不属于实例
    ② final 常量
    ③ private 修饰的方法
 */
public class Student extends Person{
    public void go(){
        System.out.println("Student Go");
    }

}


public class Teacher  extends Person {
}
public class Application {
    public static void main(String[] args) {
        // 类型之间的转换:父  ——>  子
        // 高(父)——————————————> 低(子)
        Person student = new Student();

        //Student 将这个对象转换成 Student 类型,我们就可以使用 Student类型的方法了

        Student obj = (Student) student;
        obj.go(); //强制转化表达类型①

        ((Student) student).go();//强制转换表达类型②

        // 子类转换成父类,可能会丢失自己本来的一些方法
        Student student01 = new Student();
        student01.go();
        Person person = student01; // 低 自动转换为 高
        // person.go 无法执行


        /*
        1.父类引用指向子类对象
        2.把子类转换为父类,向上自动转型,可能会导致方法缺失
        3.把父类转换为子类,向下转型,需要强制转换
        4.方便方法的调用,减少代码的重复
         */


        /*
        // Object > String
        // Object > Person > Student
        // Object > Person > Teacher

        // System.out.println(X instanceof Y); 编译能否通过,取决于 X 和 Y 之间是否存在父子关系

        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(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);//编译失败

 */

    }
}
posted @ 2025-01-27 22:57  EndeavorX  阅读(29)  评论(0)    收藏  举报