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
}
}

Class对象的isAssignableFrom方法的更多相关文章

  1. Class的isAssignableFrom方法

    Class类的isAssignableFrom是个不常用的方法,感觉这个方法的名字取得不是很好,所以有必要在此解析一下,以免在看源码时产生歧义,这个方法的签名如下: public native boo ...

  2. Java中isAssignableFrom()方法与instanceof()方法用法

    一句话总结: isAssignableFrom()方法是从类继承的角度去判断,instanceof()方法是从实例继承的角度去判断. isAssignableFrom()方法是判断是否为某个类的父类, ...

  3. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

  4. 关于 XMLHttpRequest对象的onreadyStateChange方法

    最近做了一个Ajax的demo,前台用HTML+javascript,后台用一个servlet来响应,流程如下: 页面点击链接事件,由js捕获,生成一个请求到后台,servlet处理后给出响应信息,并 ...

  5. 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...

  6. getSelection、range 对象属性,方法理解,解释

    网上转了一圈发现没有selection方面的解释,自己捣鼓下 以这段文字为例子.. <p><b>法国国营铁路公司(SNCF)20日承认,</b>新订购的2000列火 ...

  7. js中Number对象与MATH方法整理总结

    W3C的文档: Number 对象属性 属性 描述 constructor 返回对创建此对象的 Number 函数的引用. MAX_VALUE 可表示的最大的数. MIN_VALUE 可表示的最小的数 ...

  8. 【翻译十六】java-固定对象的定义方法

    A Strategy for Defining Immutable Objects The following rules define a simple strategy for creating ...

  9. [Effective JavaScript 笔记]第28条:不要信赖函数对象的toString方法

    js函数有一个非凡的特性,即将其源代码重现为字符串的能力. (function(x){ return x+1 }).toString();//"function (x){ return x+ ...

随机推荐

  1. jquery输入框自动提示

    1. 下载jar包:jquery.autocomplete.js 2. 页面内容:<script type="text/javascript" src="../jq ...

  2. 洛谷 P1016 旅行家的预算 模拟+贪心

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 总结 题面 题目链接 P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车 ...

  3. sas信用评分之第二步变量筛选

    sas信用评分之第二步变量筛选 今天介绍变量初步选择.这部分的内容我就只介绍information –value,我这次做的模型用的逻辑回归,后面会更新以基尼系数或者信息熵基础的筛选变量,期待我把. ...

  4. typroa 和markdown基操

    目录 标题 一级标题 二级标题 字体 图片 来插入图片,如在同意文件夹上,可直接加图片名 数学公式 编辑表格 标题 一级标题 二级标题 三级标题 无序标题 *加空格,无序标题 也可以使用ctrl = ...

  5. JavaScript--模拟百度搜索下拉li

    上效果: 主要思路: 函数indexOf() .join().innerHTML的使用,还有 用完的数组要清空 <!DOCTYPE html> <html> <head ...

  6. 《mysql必知必会》4笔记(存储过程、游标、触发器、事务、全球化本地化、权限、数据库维护、性能)

    二十三:使用存储过程: 1:mysql 5添加了对存储过程的支持.很多时候,一个完整的操作需要多条语句才能完成.存储过程简单来说,就是为以后的使用而保存的一条或多条mysql语句的集合,可将其视为批文 ...

  7. shell学习(20)- xargs

    xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具. xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据. xargs 也可以将单行或多 ...

  8. 提高webpack的构建速度的几种方法概括

    通过externals配置来提取常用库 利用DllPlugin和DllReferencePlugin预编译资源模块,通过DllPlugin来对那些我们引用但是绝对不会修改的npm包来进行预编译,再通过 ...

  9. GIT 用RSA加密方式来记住密码

    ssh-kegen -t rsa -C "你的帐号";//生成rsa的公钥和密钥 当然这个要在GNU环境下来执行,要是用Windows的CMD是不可以的(不输入DIR时),感觉是因 ...

  10. 2018-12-25-C#-使用转换语义版本号

    title author date CreateTime categories C# 使用转换语义版本号 lindexi 2018-12-25 09:25:41 +0800 2018-06-29 12 ...