//以下是他们的关系,Object最大,在最外面,其次是Person.... // Object > String // Object > Person > Teacher // Object > Person > Student 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); 编译报错,无法运行
能不能编译通过,看‘=’左边类型与instanceof后面的类型是否是继承关系(父子关系)。运行结果是true还是false,看‘=’右边与instanceof后面的类型是否是继承关系(父子关系)。