谦谦君子,温润如玉!桃李不言,下自成蹊!

why example

instance of类型转换

import oop.demo05继承.Teacher;
import oop.demo06多态.Person;
import oop.demo06多态.Student;

public class Appliaction {
    public static void main(String[] args) {

        //Object>String
        //Object>Person>Teacher
        //Object >Person >Studnt
        Object object = new Student();
        
        
        //System.out.println(x instanceof y);两者之间有无关系
        
        
        
        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);//编译报错
    }
}

/*
输出结果:
true
true
true
false
false
========================
true
true
true
*/
public class Appliaction{
    
    public static void main(String[] args){
        
        Person obj = new Student();
        
        //高                低
        
        //student将这个对象转换为Studen类型,我们就可以使用Student类型的方法了!
        Student student=(Student)obj;
        student.go();
        //((Student)obj).go();
    }
}
posted @ 2020-09-27 20:08  FettersLove  阅读(125)  评论(0)    收藏  举报
Live2D