首页 > 其他 > 详细

instanceof关键字

时间:2021-06-21 15:38:16      阅读:22      评论:0      收藏:0      [点我收藏+]

 

package com.oop;

import com.oop.demo04.Person;
import com.oop.demo04.Student;
import com.oop.demo04.Teacher;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object >Person>Teacher
        //Object > Person>Student
       Object object = new Student();
        //System.out.println("X instanceof Y");编译能不能过,取决于存不存在父子关系
        //结果取决于X所指向的实际类型是否为Y的子类型
        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("=====================");

        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);//编译错误
        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("=====================");
    }
}

 

instanceof关键字

原文:https://www.cnblogs.com/swfsa/p/14912374.html

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