java - 反射和接口(interface)

标签 java reflection

我有一个间接实现接口(interface)的类(它的父类(super class)或 super 类实现可以实现),我想知道一个类是否实现了接口(interface)。

用编码术语来说:

      import java.lang.reflect.Type;

        class My implements MyName{

        }

        public class Tset extends My implements yourName{
        public static void main(String[] args) {
            Class[] allClassAndInterfaces = Tset.class.getInterfaces();
    //but allClassAndInterfaces is not giving me the MyName Interface 
    //although it gives me YourName Interface
System.out.println(ArrayUtils.toString(lits));
        }
}

最佳答案

Class 类中有一个名为 isAssignableFrom 的特定方法,它可以完成您想要的操作。在您的情况下,这应该返回 true:

 MyName.class.isAssignableFrom(My.class);

关于java - 反射和接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7662535/

相关文章:

java - 设计模式如何在使用 List 时避免 instanceOf

java - 在本地创建的计时器上调用 cancel() 或清除

Java - 将分钟和秒更改为时间格式? (毫米:ss)

java - 使用反射时处理或转换从 method.invoke() 返回的对象

java - 如何在 Java 中有条件地(通过字符串)创建实例?

kotlin - 动态(在运行时)调用数组的迭代器运算符函数

c# - Mono 的 PropertyInfo.GetValue() 的替代品?

java - 设计模式问题

java - 无法通过反射调用带有 varargs 参数的方法 - NoSuchMethodException

java - 为什么 JavaFX 舞台/窗口/应用程序在更改场景后不刷新?