MENU

instanceof

June 5, 2018 • Read: 2981 • 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
Leave a Comment