一、Instance of 用法
1、格式
对象 instanceof 类名 //右边可以是普通类、抽象类、接口
2、返回类型---Boolean
如果左边的对象是右边的实例化,就是说右边是创建对象的类、继承父类、实现接口返回TRUE。否则返回false。
二、例子
interface A {
}
class C {
}
class D extends C{
}
class B extends D implements A {
}
public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
结果
b is an instance of A
b is an instance of C