首页 > 其他分享 >instanceof

instanceof

时间:2022-12-01 21:35:08浏览次数:31  
标签:instanceof System Student println true out

	//Object > String
        //Object > Person > Student
        //Object > Person > Teacher

        //System.out.println(X instanceof Y);//能不能编译通过取决于X和Y之间是否存在父子关系

        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false

        System.out.println("============================================");

        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);  //编译报错!

        System.out.println("============================================");

        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);//编译报错!
        //System.out.println(student instanceof String);//编译报错!

标签:instanceof,System,Student,println,true,out
From: https://www.cnblogs.com/cyyyds/p/16942831.html

相关文章

  • 检测数据类型的方法 instanceof typeof 终极检测类型 Object.prototype.toSt
    检测数据类型的方法:    1.instanceof     2.typeof        3.终极检测类型Object.prototype.toString.call()   =============......
  • Day23:instanceof 和类型转换
    多态中的转型转型当父类的不能调用子类方法时,我们可以将父类转换成子类,然后就可以调用子类的方法。转型分为两种:向上转型:父类引用指向子类对象;向下转型:父类引用转为子......
  • instanceof和类型转换
    instanceofpackagecom.oop.demo06;publicclassPerson{}//==========================packagecom.oop.demo06;publicclassTeacherextendsPerson{}......
  • Day8-3 instanceof和类型转换
    instanceof和类型转换判断一个对象是什么类型:instanceofxinstanceofy packagecom.oop; ​ importcom.oop.demo06.Person; importcom.oop.demo06.Student; im......
  • Javascript中字符串的instanceof String的结果
    如果是单纯的字符串赋给变量,虽然类型为string,但是instanceofString是false,并不是String对象,因为没有创建实例. 而这种new一个String实例则instanceof是属于String......
  • 多态性、instanceof关键字
    目录多态性向下转型instanceof关键字多态性理解多态性:可以理解为一个事物的多种形态。何为多态性:对象的多态性:父类的引用指向子类的对象(或子类的对象赋给父类的引用)多......
  • instanceof 运算符
    instanceof是二元运算符,左边是对象,右边是类;当对象是右面类或子类所创建对象时,返回true;否则,返回false。【示例】使用instanceof运算符进行类型判断publicclassTe......
  • #yyds干货盘点# 前端歌谣的刷题之路-第一百六十六题-instanceOf
     前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了......
  • 转型和instanceof关键字
    向上转型向下转型instanceof关键字向下转型向下转型和继承和覆写构成了多态,多态的出现使得父类成为一个接口,屏蔽了不同子类的差异性,为统一的变成成为了可能没有用......
  • Demo59_多态_instanceof_亲子鉴定
      //亲子鉴定instanceof/*已知条件:动物类是dog类,mao类,people类的父类Object是所有类的祖宗类,String是祖宗类的其中一个类Object-->动物-->dog,mao,people*/packagec......