isAssignableFrom

在看一个开源代码时,在加载完某个Class对象后,经常会使用 java.lang.Class#isAssignableFrom 来校验下。

之前真没有注意过Class对象中还有个这样的方法,下面是是这个方法的定义:

public native boolean isAssignableFrom(Class<?> cls);

他到底是干什么用的呢?

先来看下Java源码中的注释:

java.lang.Class
public boolean isAssignableFrom(@org.jetbrains.annotations.NotNull Class<?> cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
Parameters:
cls - the Class object to be checked
Returns:
the boolean value indicating whether objects of the type cls can be assigned to objects of this class
Throws:
NullPointerException - if the specified Class parameter is null.
Since:
JDK1.1

哦。。。

就是判断下该Class对象所描述的class和interface和参数cls所描述的class和interface是不是同样的,或者是参数cls所描述的class和interface的超类/超接口。

instanceof

立马让我联想到Java的关键字: instanceof

instanceof的作用是判断对象是不是指定的类型或子类型。

isInstance

如果你稍微认真点,还会发现Class对象中还有个关于对象继承关系的方法:

public native boolean isInstance(Object obj);

对应的注释:

This method is the dynamic equivalent of the Java language instanceof operator.

等价于关键字 instanceof

使用样例

虽然这里既有对象,又有Class对象,但是他们所判断的都是,对象与Class对象所描述的class/interface的关系,而不是与Class对象关系

 public class TestInstance extends Test {

     public static void main(String[] args) {
// Object test = new Object();
// TestInstance test = new TestInstance();
Test test = new Test();
System.out.println(test instanceof Test);                   //true
System.out.println(test instanceof TestInstance);              //false System.out.println(Test.class.isAssignableFrom(TestInstance.class));  //true
System.out.println(TestInstance.class.isAssignableFrom(Test.class));  //false
System.out.println(Test.class.isInstance(test));              //true
System.out.println(TestInstance.class.isInstance(test));         //false
}
}

最新文章

  1. EC笔记,第二部分:10.让=返回指向*this的引用
  2. 如何给main传参数
  3. ios基础篇(二十七)—— Json解析
  4. 前端:js
  5. 对冲的艺术——delta中性交易
  6. CSS Sprites (CSS图像拼合技术)教程工具
  7. iOS开发小技巧--iOS8之后的cell自动计算高度
  8. Nexus4_文件名乱码
  9. 2.C#基础篇--&gt;数据类型
  10. 剑指offer—第三章高质量代码(数值的整数次方)
  11. 记一次apt-get无法安装git的问题
  12. [C#参考]属性
  13. github 用git bash上传项目 最后提示 Everything up-to-date 但没传上去
  14. DTW和DBA
  15. git错误记录及解决
  16. [Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array
  17. developer的996,需要谁来拯救
  18. python之模块3
  19. 【Nodejs】Expressのサンプルについて
  20. Vue-ui常用组件库整理

热门文章

  1. StringBuilder / StringBuffer类
  2. SpingBoot myBatis neo4j整合项目案例
  3. python ndarray相关操作:转置、翻转
  4. AC自动机fail树小结
  5. 洛谷 P4205 [NOI2005]智慧珠游戏 DFS
  6. PHP学习(语言结构语句)
  7. objectarx之模型空间
  8. codevs1506 传话
  9. dsadsa
  10. windows和linux下读取文件乱码的终极解决办法!