Java - 如何检查类是否从某个类或接口继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16210369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How Can I Check If A Class Is Inheriting From Some Class Or Interface?
提问by Bitterblue
I need to check:
我需要检查:
public static boolean check(Class<?> c, Class<?> d)
{
if (/* c inherits from d */)
return true;
else
return false;
}
How can I do that ?
我怎样才能做到这一点 ?
And is that possible without c.newInstance()
?
这可能没有c.newInstance()
吗?
第一次标题错了。现在是正确的。
回答by Kevin Bowersox
Use isAssignableFrom
利用 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.
确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。如果是,则返回 true;否则返回false。如果此 Class 对象表示原始类型,则如果指定的 Class 参数正是此 Class 对象,则此方法返回 true;否则返回false。
回答by PermGenError
There is a method called Class#isInterface()in Class
有一个名为方法类#isInterface()在类
if (c.isInterface())
return true;
回答by user2256686
Try this out
试试这个
if(c.isAssignableFrom(d)){
return true;
} else {
return false;
}
回答by saeed
if (c.isInterface()) return true;
isInterface
是接口
public boolean isInterface()
Determines if the specified Class object represents an interface type. Returns: true if this object represents an interface; false otherwise. sAssignableFrom
确定指定的 Class 对象是否表示接口类型。返回: 如果此对象表示一个接口,则返回 true;否则为假。可分配自
public boolean isAssignableFrom(Class<?> cls)
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 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。如果是,则返回 true;否则返回false。如果此 Class 对象表示原始类型,则如果指定的 Class 参数正是此 Class 对象,则此方法返回 true;否则返回false。
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
具体来说,此方法测试是否可以通过标识转换或通过扩展引用转换将指定的 Class 参数表示的类型转换为此 Class 对象表示的类型。有关详细信息,请参阅 Java 语言规范的第 5.1.1 和 5.1.4 节。
Parameters: cls - the Class object to be checked Returns: the boolean value indicating whether objects of the type cls can be assigned to objects of this class Throws: NullPointerException - if the specified Class parameter is null. Since: JDK1.1
参数: cls - 要检查的 Class 对象 返回: 指示 cls 类型的对象是否可以分配给此类的对象的布尔值 抛出: NullPointerException - 如果指定的 Class 参数为 null。从:JDK1.1
回答by Apurv
How about
怎么样
According to docs:
根据文档:
Determines if the specified Class object represents an interface type. Returns: true if this object represents an interface; false otherwise.
确定指定的 Class 对象是否表示接口类型。返回: 如果此对象表示一个接口,则返回 true;否则为假。
回答by Suresh Atta
have you tried with
你有没有试过
c.isInterface()???
from docs
来自文档
Determines if the specified Class object represents an interface type.
确定指定的 Class 对象是否表示接口类型。