Java - 如何检查一个类是否继承自某个类或接口(interface)?

标签 java reflection instances

我需要检查:

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.

Source

关于Java - 如何检查一个类是否继承自某个类或接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16210369/

相关文章:

c# - 是否可以反编译整个 .NET 应用程序?

c# - Reflection.Emit 泛型基类泛型方法调用

c++ - 一次运行一个程序的多个实例 C++

java - 在构造函数中使用参数化类型从名称实例化类

python - 使用 boto 从 AWS 实例获取标签

haskell - Hackage的GenProg示例中的实例错误

java - 如果测试运行正常,为什么我会收到反射异常 NoSuchMethodException

java - jsp中获取表单数据

java - android gms 大方法计数。如何避免呢?

java - 如何在 Eclipse 中完全禁用 Oomph?