从实现类中获取接口名称 [英] Get the interface name from the implementation class
本文介绍了从实现类中获取接口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
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?
推荐答案
使用Reflection你可以调用 Class.getInterfaces()
方法,它返回您的类实现的接口数组
。
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]);
获取名称
System.out.println(list.getClass().getInterfaces()[0].getSimpleName);
这篇关于从实现类中获取接口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文