Java关键字用法
instanceof
boolean result = obj instanceof Class
obj表示一个对象
Class表示一个类或者一个接口
当obj是Class的对象,或者是其直接或间接的子类的对象,或者是其接口的实现类,均返回true
int i = 1;
System.out.println(i instanceof Integer);
System.out.println(i instanceof Object);
如果 obj 为 null,那么将返回 false
System.out.println(null instanceof Integer);
ArrayList arrayList = new ArrayList();
System.out.println(arrayList instanceof List);
List list = new ArrayList();
System.out.println(list instanceof ArrayList);
public class Main2 {
public static void main(String[] args) {
Person2 p1 = new Person2();
Person2 p2 = new student2();
student2 s1 = new student2();
System.out.println(p1 instanceof Person2);
System.out.println(p2 instanceof Person2);
System.out.println(p2 instanceof student2);
System.out.println(s1 instanceof Person2);
System.out.println(p1 instanceof student2);
}
}
class Person2{
}
class student2 extends Person2{
}
pass