zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    第一次sprint团队贡献分改
    第一个Sprint冲刺事后诸葛报告
    第一个Sprint冲刺第十天
    第一个Sprint冲刺第九天
    第一个Sprint冲刺第八天
    第一个Sprint冲刺第七天
    第一个Sprint冲刺第六天
    第一个Sprint冲刺第五天
    第一个Sprint冲刺第四天
    第一个Sprint冲刺第三天
  • 原文地址:https://www.cnblogs.com/FettersLove/p/13741549.html
Copyright © 2011-2022 走看看