MENU

instanceof

June 5, 2018 • Read: 241 • Java

insatceof判断一个对象是否是一个类的实例,返回值为boolean类型

class A{
    
}
class B extends A{
    
}
public static void main(String args[]){
    A a1 = new A();
    System.out.println(a1 instanceof A);//ture
    System.out.println(a1 instanceof B);//false
    A a2 = new B();
    System.out.println(a2 instanceof A);//ture
    System.out.println(a2 instanceof B);//ture
}
Archives Tip
QR Code for this page
Tipping QR Code