我需要检查:
public static boolean check(Class<?> c, Class<?> d)
{
if (/* c inherits from d */)
return true;
else
return false;
}
我该怎么做?
没有 c.newInstance()
有可能吗?
第一次看错标题了。现在是正确的。
最佳答案
使用isAssignableFrom
if(d.isAssignableFrom(c)){
// then d is a superclass of c
// in other words, c inherits d
}
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.
关于Java - 如何检查一个类是否继承自某个类或接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16210369/