instance of和类型转换

强制类型转换:由高到低

向下转型

(类型、类名)变量名

自动类型转换:由低到高

向上转型

实例:

public class Application {
    public static void main(string[] args) {
        Person person=new student();
        person. sleep();
        Student student= (Student) person;//强制转换 由高到低
        student. study();
        Person person1=student;//自动转换由低到高
        person1. sleep();
            }
    }

instanceof判断类型

通过instanceof判断对象是什么类型

两类之间存在父子关系则instanceof会ok,否则不行

写法: object instanceof student --判断右边的类型是否是左边类型的实例或存在父子关系

有上下继承关系时才会互相转/判断为true,兄弟关系不能互相转换(继承相同父类的子类)
instance和类型转换.jpg

最后修改:2022 年 01 月 06 日
如果觉得我的文章对你有用,请随意赞赏