instanceof 介绍

instanceof 它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。

代码测试

class Parent {
    public String name;
}

class Child extends Parent {
    public int age;
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        System.out.println("c instanceof Child:"+ (c instanceof Child));
        System.out.println("c instanceof Parent:"+ (c instanceof Parent));
    }
}

结果:
c instanceof Child:true
c instanceof Parent:true

相关文章:

  • 2021-08-22
  • 2021-10-03
  • 2021-11-29
猜你喜欢
  • 2021-10-20
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
相关资源
相似解决方案