【发布时间】:2013-01-15 11:12:08
【问题描述】:
例子:
List<String> list = new ArrayList<String>();
//This would give me the class name for the list reference variable.
list.getClass().getSimpleName();
我想从list 引用变量中获取接口名称。
有没有办法做到这一点?
【问题讨论】:
例子:
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();
【讨论】:
Class aClass = ... //obtain Class object.
Class[] interfaces = aClass.getInterfaces();
【讨论】: