Get the interface name from the implementation cla

2019-01-27 18:19发布

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条回答
混吃等死
2楼-- · 2019-01-27 18:43

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

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

To get just the name

System.out.println(list.getClass().getInterfaces()[0].getSimpleName);
查看更多
疯言疯语
3楼-- · 2019-01-27 19:04
Class  aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces();
查看更多
登录 后发表回答