isAssignableFrom方法浅析

2023-05-16

源码

 /**
     * Determines if the class or interface represented by this
     * {@code Class} object is either the same as, or is a superclass or
     * superinterface of, the class or interface represented by the specified
     * {@code Class} parameter. It returns {@code true} if so;
     * otherwise it returns {@code false}. If this {@code Class}
     * object represents a primitive type, this method returns
     * {@code true} if the specified {@code Class} parameter is
     * exactly this {@code Class} object; otherwise it returns
     * {@code false}.
     *
     * <p> Specifically, this method tests whether the type represented by the
     * specified {@code Class} parameter can be converted to the type
     * represented by this {@code Class} object via an identity conversion
     * or via a widening reference conversion. See <em>The Java Language
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
     *
     * @param cls the {@code Class} object to be checked
     * @return the {@code boolean} value indicating whether objects of the
     * type {@code cls} can be assigned to objects of this class
     * @exception NullPointerException if the specified Class parameter is
     *            null.
     * @since JDK1.1
     */
    public native boolean isAssignableFrom(Class<?> cls);

翻译

Class a = A.class;
Class b = B.class;
boolean c = a.isAssignableFrom(b)

如果当前类对象(对应a)表示的类或者接口 是 参数类(对应b)表示的类或者接口 相同类型或者父类或者父接口。则返回true;否则返回false。当前类对象(对应a)表示的原始类型(原始类型一共有8种,它们分别是char,boolean,byte,short,int,long,float,double),如果参数类(对应b)和当前类对象(对应a)类型一样则返回true;否则返回false。

确切的说,这个方法用于判断参数类(对应b)表示的类型能否转换为当前类对象(对应a)表示的类型通过标识转换或者拓宽和窄化简单类型转换。详情请参阅 Java语言规范第5.1.1和5.1.4节。

实例 

public class ChinaMadeCar extends ChinaMadeCarA implements DeviceA {

}

public class ChinaMadeCarA extends Test{

}

public class Test   {

}

public interface DeviceA {

}


 

public class Chery extends ChinaMadeCar implements Car,Device{
    public static void main(String[] args) {
        //相同类  返回true
        boolean b0 = Chery.class.isAssignableFrom(Chery.class);
        //当前类对象直接父类ChinaMadeCar 返回true
        boolean b1 = ChinaMadeCar.class.isAssignableFrom(Chery.class);
        //当前类对象直接实现的接口Car 返回true
        boolean b2 = Car.class.isAssignableFrom(Chery.class);
        //当前类对象直接实现的接口Device 返回true
        boolean b3 = Device.class.isAssignableFrom(Chery.class);
        //父类ChinaMadeCar直接实现的接口DeviceA 返回true
        boolean b4 = DeviceA.class.isAssignableFrom(Chery.class);

        //间接父类ChinaMadeCarA(父类ChinaMadeCar的直接父类) 返回false
        boolean b5 = ChinaMadeCarA.class.isAssignableFrom(Chery.class);
        boolean b6 = Test.class.isAssignableFrom(Chery.class);
        System.out.println("b0="+b0+"\n"+"b1="+b1+"\n"
                +"b2="+b2+"\n"+"b3="+b3+"\n"+"b4="+b4+"\n"
                +"b5="+b5+"\n"
                +"b6="+b6);
    }
}
//        控制台输出结果:
//        b0=true
//        b1=true
//        b2=true
//        b3=true
//        b4=true
//        b5=true
//        b6=true
//        b7=false

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

isAssignableFrom方法浅析 的相关文章

  • IDEA添加serialVersionUID

    打开IDEA中的 Setting gt Editor gt Inspections 选项中 xff0c java gt Serialization issues gt 将Serializable class without 39 seria
  • java序列化

    http www cnblogs com szlbm p 5504166 html Java对象表示方式1 xff1a 序列化 反序列化和transient关键字的作用 平时我们在Java内存中的对象 xff0c 是无 法进行IO操作或者网
  • Java之Collections.emptyList()、emptySet()、emptyMap()的作用和好处以及要注意的地方。

    https blog csdn net qq 27093465 article details 65444622 先说明一下好处有哪些 xff1a 1 xff0c 如果你想 new 一个空的 List xff0c 而这个 List 以后也不
  • java String ... valuese 什么意思

    jdk1 5的新特性 xff1a 变长变量 其实这种定义就类似一个数据的定义 xff0c 可以不用给它的长度加以限制 xff0c 可以传入任意多个参数 比用数据更灵活一些 xff0c 不会出现一些数组越界等的异常 如 xff1a getTy
  • debug技巧和使用

    介绍一种能debug到HashMap内部数据结构的方法 https blog csdn net victor cindy1 article details 52336983 1 这里以一个web工程为例 xff0c 点击图中按钮开始运行we
  • Linux中如何添加/删除FTP用户并设置权限?

    以阿里云服务器为例 xff0c 在linux中添加ftp用户 xff0c 并设置相应的权限 xff0c 操作步骤如下 xff1a 1 环境 xff1a ftp为vsftp 被设置用户名为test 被限制路径为 alidata www tes
  • 解析IOS二进制格式的bplist

    关于二进制格式的plist xff0c 搜到一篇博客 详解Binary Plist格式 xff0c 介绍的很详细 xff0c 但是结合github上关于一份解析bplist的代码通过结果实际来看 xff0c 博客中解析对象表的说明出现了问题
  • Java中的String,StringBuilder,StringBuffer三者的区别

    最近在学习Java的时候 xff0c 遇到了这样一个问题 xff0c 就是String StringBuilder以及StringBuffer这三个类之间有什么区别呢 xff0c 自己从网上搜索了一些资料 xff0c 有所了解了之后在这里整
  • SynthesizedAnnotation

    标识组合注解 该接口没有实现类 xff0c 具体用法待研究
  • autowire注解源码解析

    引用 xff1a https blog csdn net wang704987562 article details 80868368
  • ConcurrentHashMap解析

    https www cnblogs com ITtangtang p 3948786 html java util concurrent xff08 j u c xff09 源码阅读
  • Java中getResourceAsStream的用法

    https www cnblogs com macwhirr p 8116583 html 首先 xff0c Java中的getResourceAsStream有以下几种 xff1a 1 Class getResourceAsStream
  • Map var2 = this.bfgInstancesByKey; synchronized(this.bfgInstancesByKey) { 疑惑

    org springframework beans factory access SingletonBeanFactoryLocator useBeanFactory public BeanFactoryReference useBeanF
  • spring父子容器

    https www jb51 net article 132197 htm http www cnblogs com kevin yuan p 6404702 html https blog csdn net user xiangpeng
  • ContextLoaderListener与RequestContextListener配置问题

    https blog csdn net yanweju article details 70622313 在SSH2 SSM等web应用开发框架的配置过程中 xff0c 因为都要用到Spring xff0c 所以 xff0c 往往我们首先都
  • Class的isAssignableFrom方法

    https www cnblogs com hzhuxin p 7536671 html Class类的isAssignableFrom是个不常用的方法 xff0c 感觉这个方法的名字取得不是很好 xff0c 所以有必要在此解析一下 xff
  • dto层与model层的区别

    Model层是面向业务的 xff0c 我们是通过业务来定义Model的 而DTO是面向界面UI的 xff0c 是通过UI的需求来定义的 通过DTO我们实现了表现层与Model之间的解耦 xff0c 表现层不引用Model 如果开发过程中我们
  • 常用命令 ctags的使用

    1 命令ctags ctags的功能 xff1a 扫描指定的源文件 xff0c 找出其中所包含的语法元素 xff0c 并将找到的相关内容记录下来 ctags R 扫描当前目录及所有子目录 xff08 递归向下 xff09 中的源文件 结合v

随机推荐