说明:
本系列博客是本人在工作中遇到的一些问题的整理,其中有些资料来源网络博客,有些信息来自出版的书籍,掺杂一些个人的猜想及验证,总结,主要目的是方便知识的查看,并非纯原创。本系列博客会不断更新。原创不容易,支持原创。对于参考的一些其他博客,会尽量把博客地址列在博客的后面,以方便知识的查看。
?
Class中的isAssignableFrom(clazz)表示入参是否能自动转换成调用该方法的类型,入参跟类型都是Class类型。
/**
?* Created with IntelliJ IDEA.
?* User: yejunwu123@gmail.com
?* Date: 2014-08-17 21:25
?* Description:测试Class.assignableFrom()方法
?*/
public class IsAssignableFromTest {
??? public static void main(String[] args) {
??????? //创建对象
??????? A ab = new B();
??????? A ac = new C();
?
??????? B bb = new B();
??????? B bc = new C();
?
??????? C cc = new C();
?
??????? D dd = new D();
?
??????? //A.class.isAssignableFrom(..)
??????? System.out.println("A.class.isAssignableFrom(ab) = " + (A.class.isAssignableFrom(ab.getClass())));
??????? System.out.println("A.class.isAssignableFrom(ac) = " + (A.class.isAssignableFrom(ac.getClass())));
??????? System.out.println("A.class.isAssignableFrom(bb) = " + (A.class.isAssignableFrom(bb.getClass())));
??????? System.out.println("A.class.isAssignableFrom(bc) = " + (A.class.isAssignableFrom(bc.getClass())));
??????? System.out.println("A.class.isAssignableFrom(cc) = " + (A.class.isAssignableFrom(cc.getClass())));
??????? System.out.println("A.class.isAssignableFrom(dd) = " + (A.class.isAssignableFrom(dd.getClass())));
??????? //以下这行代码会抛出NullPointerException
??????? //System.out.println("A.class.isAssignableFrom(dd) = " + (A.class.isAssignableFrom(null)));
??????? System.out.println("-------------------------------------");
??????? //B.class.isAssignableFrom(..)
??????? System.out.println("B.class.isAssignableFrom(ab) = " + (B.class.isAssignableFrom(ab.getClass())));
??????? System.out.println("B.class.isAssignableFrom(ac) = " + (B.class.isAssignableFrom(ac.getClass())));
??????? System.out.println("B.class.isAssignableFrom(bb) = " + (B.class.isAssignableFrom(bb.getClass())));
??????? System.out.println("B.class.isAssignableFrom(bc) = " + (B.class.isAssignableFrom(bc.getClass())));
??????? System.out.println("B.class.isAssignableFrom(cc) = " + (B.class.isAssignableFrom(cc.getClass())));
??????? System.out.println("B.class.isAssignableFrom(dd) = " + (B.class.isAssignableFrom(dd.getClass())));
??????? //以下这行代码会抛出NullPointerException
??????? //System.out.println("B.class.isAssignableFrom(dd) = " + (B.class.isAssignableFrom(null)));
??????? System.out.println("-------------------------------------");
??????? //C.class.isAssignableFrom(..)
??????? System.out.println("C.class.isAssignableFrom(ab) = " + (C.class.isAssignableFrom(ab.getClass())));
??????? System.out.println("C.class.isAssignableFrom(ac) = " + (C.class.isAssignableFrom(ac.getClass())));
??????? System.out.println("C.class.isAssignableFrom(bb) = " + (C.class.isAssignableFrom(bb.getClass())));
??????? System.out.println("C.class.isAssignableFrom(bc) = " + (C.class.isAssignableFrom(bc.getClass())));
??????? System.out.println("C.class.isAssignableFrom(cc) = " + (C.class.isAssignableFrom(cc.getClass())));
??????? System.out.println("C.class.isAssignableFrom(dd) = " + (C.class.isAssignableFrom(dd.getClass())));
??????? //以下这行代码会抛出NullPointerException
??????? //System.out.println("C.class.isAssignableFrom(dd) = " + (C.class.isAssignableFrom(null)));
??????? System.out.println("-------------------------------------");
??????? //D.class.isAssignableFrom(..)
??????? System.out.println("D.class.isAssignableFrom(ab) = " + (D.class.isAssignableFrom(ab.getClass())));
??????? System.out.println("D.class.isAssignableFrom(ac) = " + (D.class.isAssignableFrom(ac.getClass())));
??????? System.out.println("D.class.isAssignableFrom(bb) = " + (D.class.isAssignableFrom(bb.getClass())));
??????? System.out.println("D.class.isAssignableFrom(bc) = " + (D.class.isAssignableFrom(bc.getClass())));
??????? System.out.println("D.class.isAssignableFrom(cc) = " + (D.class.isAssignableFrom(cc.getClass())));
??????? System.out.println("D.class.isAssignableFrom(dd) = " + (D.class.isAssignableFrom(dd.getClass())));
??????? //以下这行代码会抛出NullPointerException
??????? //System.out.println("D.class.isAssignableFrom(dd) = " + (D.class.isAssignableFrom(null)));
??? }
}
?
对于Class的isAssignableFrom(clazz),入参是被检测的对象,A.class.isAssignableFrom(ab.getClass()),ab.getClass()为被检测对象,只要调用该方法的类型(A)
?
是被检测对象(ab.getClass())的本身类型、父类或是被检测对象实现的接口,则表达式返回true,否则返回false。