isAssignableFrom(Class<?> c)
- 标识 “当前Class 是否是给定的 Class 的超类或者超接口”。是 返回true,否则返回false。
- 标识 “当前Class 是否与定的 Class 的相同”。是 返回true,否则返回false。
- 如果 “如果该 Class 表示一个基本类型,且指定的 Class 参数正是该 Class 对象”,是则返回true,否则放回false。
demo
public static void main(String[] args){
System.out.println(String.class.isAssignableFrom(Object.class));
System.out.println(Object.class.isAssignableFrom(String.class));
System.out.println("----------------------------------------");
System.out.println(int.class.isAssignableFrom(int.class));
System.out.println(int.class.isAssignableFrom(Object.class));
System.out.println(Object.class.isAssignableFrom(int.class));
System.out.println(Integer.class.isAssignableFrom(Object.class));
System.out.println(Object.class.isAssignableFrom(Integer.class));
System.out.println("----------------------------------------");
System.out.println(Person.class.isAssignableFrom(Object.class));
System.out.println(Object.class.isAssignableFrom(Person.class));
System.out.println(Person.class.isAssignableFrom(Person.class));
System.out.println(Person.class.isAssignableFrom(Serializable.class));
System.out.println(Serializable.class.isAssignableFrom(Person.class));
System.out.println(Person.class.isAssignableFrom(SuperA.class));
System.out.println(SuperA.class.isAssignableFrom(Person.class));
System.out.println("----------------------------------------");
System.out.println(List.class.isAssignableFrom(ArrayList.class));
System.out.println(ArrayList.class.isAssignableFrom(List.class));
}
class Person implements Serializable, A{}
interface A extends SuperA{}
interface SuperA{}
log
false
true
======================
true
false
false
false
true
======================
false
true
true
false
true
false
true
=======================
true
false
网友评论