ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

instanceof(类型)

instanceof(类型)

instanceof(检测左侧的对象是否为右侧类或接口的实例)

public class Main
{public static void main(String[] args){//方法的调用只和左边定义的数据类型有关Object object=new teacher();System.out.println(object instanceof stu);//trueSystem.out.println(object instanceof teacher);//trueSystem.out.println(object instanceof Object);//trueSystem.out.println(object instanceof String);//false}}

看new 的对象,new谁就说明那条线里面跟谁是父子关系,new Student 那么那条线就和Teacher没什么关系 instance Teacher 就是false
image

返回列表