siyuliu  

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
image

posted on 2025-11-11 21:21  nnnnnnunn  阅读(0)  评论(0)    收藏  举报