java - 为什么 ReflectionChild.class.isInstance(Class.class) 不正确?

标签 java class

为什么 ReflectionChild.class.isInstance(Class.class) 不为 true?

我们在反射中知道 Class.class.isInstance(Class.class) 是 true。现在请参阅下面的代码片段。

   ReflectionChild ch = new ReflectionChild(); //Take random class

    if(ch.getClass()==ReflectionChild.class){
        System.out.println("ch.getClass()==ReflectionChild.class");
    }

    System.out.println(ReflectionChild.class.getClass());
    System.out.println(Class.class);

    if(ReflectionChild.class.getClass()==Class.class){
        System.out.println("ReflectionChild.class.getClass()==Class.class");
        //System.exit(0);
    }
    if(ReflectionChild.class.isInstance(Class.class)){
        System.out.println("true");
        //System.exit(0);
    }else{
       System.out.println("false");
    }

输出是:-

ch.getClass()==ReflectionChild.class  // 1st SYSOUT

class java.lang.Class                 // 2nd SYSOUT

class java.lang.Class                 // 3rd SYSOUT  

ReflectionChild.class.getClass()==Class.class  //4th SYSOUT

false   // 5th SYSOUT

根据上面的第 4 个 SYSOUT,行 ReflectionChild.class.isInstance(Class.class) 应该为 true。但对我来说,它作为输出是 false 。

谁能解释一下吗?

最佳答案

Class.classClass 的实例,而不是 ReflectionChild
因此,isInstance() 返回 false。

关于java - 为什么 ReflectionChild.class.isInstance(Class.class) 不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484947/

相关文章:

c++ - 以抽象类作为声明 C++ 的基于范围的 for 循环

java - Tomcat8 使用 AbstractProtocol.pause 随机关闭

java - 父类(super class)中的泛型可以处理子类类型

java - JPARepository 接口(interface)是否涵盖了 Spring Boot 中 DAO 接口(interface)的职责?

function - Perl - 调用类的实例

java - 将 String 转换为 Activity 类

c++ find()用于成员函数的映射

java - 需要存储另一个领域模型的领域模型

java - spring boot mongodb连接错误

java - 创建 "noname"类实例