简体   繁体   English

从实现类获取接口名称

[英]Get the interface name from the implementation class

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. 我想从list引用变量中获取接口名称。 Is there any way possible to do that? 有没有办法做到这一点?

Using Reflection you can invoke the Class.getInterfaces() method which returns an Array of Interfaces that your class implements. 使用反射,您可以调用Class.getInterfaces()方法,该方法返回您的类实现Array of Interfaces

list.getClass().getInterfaces()[0];

To get just the name 为了得到名字

list.getClass().getInterfaces()[0].getSimpleName();
Class  aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM