从实现类中获取接口名称
示例:
List list = new ArrayList(); //This would give me the class name for the list reference variable. list.getClass().getSimpleName();
我想从list
引用变量中获取接口名称。 有什么办法可以吗?
使用Reflection可以调用Class.getInterfaces()
方法,该方法返回类实现Array of Interfaces
。
System.out.println(list.getClass().getInterfaces()[0]);
得到这个名字
System.out.println(list.getClass().getInterfaces()[0].getSimpleName);
Class aClass = ... //obtain Class object. Class[] interfaces = aClass.getInterfaces();