instanceof(检测左侧的对象是否为右侧类或接口的实例)
public class Main
{
public static void main(String[] args)
{
//方法的调用只和左边定义的数据类型有关
Object object=new teacher();
System.out.println(object instanceof stu);//true
System.out.println(object instanceof teacher);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
}
}
看new 的对象,new谁就说明那条线里面跟谁是父子关系,new Student 那么那条线就和Teacher没什么关系 instance Teacher 就是false


浙公网安备 33010602011771号