java 从实现类中获取接口名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14624298/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:52:12  来源:igfitidea点击:

Get the interface name from the implementation class

javaclassinterface

提问by Jatin Sehgal

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 listreference variable. Is there any way possible to do that?

我想从list引用变量中获取接口名称。有没有办法做到这一点?

回答by PermGenError

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

使用反射,您可以调用Class.getInterfaces()返回Array of Interfaces类实现的方法。

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

To get just the name

只得到名字

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

回答by TheWhiteRabbit

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