从实现类获取接口名称

范例:

List<String> list = new ArrayList<String>();

//This would give me the class name for the list reference variable.

list.getClass().getSimpleName();

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

回答:

使用反射,您可以调用Class.getInterfaces()返回Array

of Interfaces您的类实现的方法。

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

为了得到名字

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

以上是 从实现类获取接口名称 的全部内容, 来源链接: utcz.com/qa/402042.html

回到顶部