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();
        }
    }
    
  • 相关阅读:
    如何在SQLite中创建自增字段?
    Windows XP平台下编译boost[1.47及以上]
    智能指针的向下转型
    采用Boost::filesystem操作文件
    CodeSmith访问数据库
    std::string的一些操作
    PDF加入内嵌字体
    悟空和唐僧的对话
    收获和教训的一天配置ds1401
    vxworks的一个changlog
  • 原文地址:https://www.cnblogs.com/FettersLove/p/13741549.html
Copyright © 2011-2022 走看看