从实现类中获取接口名称

时间:2023-01-12 17:46:20

Example :

示例:

List<String> list = new ArrayList<String>();
//This would give me the class name for the list reference variable.
list.getClass().getSimpleName();

I want to get the Interface name from the list reference variable. Is there any way possible to do that?

我想从列表引用变量中获取接口名称。有什么办法可以吗?

2 个解决方案

#1


12  

Using Reflection you can invoke the Class.getInterfaces() method which returns an Array of Interfaces that your class implements.

使用Reflection可以调用Class.getInterfaces()方法,该方法返回类实现的接口数组。

System.out.println(list.getClass().getInterfaces()[0]);

To get just the name

得到这个名字

System.out.println(list.getClass().getInterfaces()[0].getSimpleName);

#2


2  

Class  aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces();

#1


12  

Using Reflection you can invoke the Class.getInterfaces() method which returns an Array of Interfaces that your class implements.

使用Reflection可以调用Class.getInterfaces()方法,该方法返回类实现的接口数组。

System.out.println(list.getClass().getInterfaces()[0]);

To get just the name

得到这个名字

System.out.println(list.getClass().getInterfaces()[0].getSimpleName);

#2


2  

Class  aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces();