首页 > 其他 > 详细

instance of类型转换

时间:2020-09-27 21:22:37      阅读:27      评论:0      收藏:0      [点我收藏+]
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();
    }
}

instance of类型转换

原文:https://www.cnblogs.com/FettersLove/p/13741549.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!