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("====================="); } }
原文:https://www.cnblogs.com/swfsa/p/14912374.html