2020-12-07isAssignableFrom

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

<pre style="margin: 0px; padding: 0px; overflow: auto; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">public native boolean isAssignableFrom(Class<?> cls);</pre>

由方法签名可见是一个本地方法,即C代码编写的。

以下是JDK中的注释:

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.

意思如下:

有两个Class类型的类象,一个是调用isAssignableFrom方法的类对象(后称对象a),以及方法中作为参数的这个类对象(称之为对象b),这两个对象如果满足以下条件则返回true,否则返回false:

a对象所对应类信息是b对象所对应的类信息的父类或者是父接口,简单理解即a是b的父类或接口

a对象所对应类信息与b对象所对应的类信息相同,简单理解即a和b为同一个类或同一个接口

测试代码:

//说明:Protocol是接口,DubboProtocol是Protocol的实现

 protocolClass.isAssignableFrom(dubboProtocolClass )) ;   //返回true
 protocolClass.isAssignableFrom(protocolClass )) ;        //返回true
 dubboProtocolClass.isAssignableFrom(protocolClass )) ;   //返回false</pre>

转载 https://www.cnblogs.com/hzhuxin/p/7536671.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。