为什么 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.class
是 Class
的实例,而不是 ReflectionChild
。
因此,isInstance()
返回 false。
关于java - 为什么 ReflectionChild.class.isInstance(Class.class) 不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484947/