9

例 :

List<String> list = new ArrayList<String>();
//This would give me the class name for the list reference variable.
list.getClass().getSimpleName();

list参照変数からインターフェイス名を取得したい。それを行う方法はありますか?

4

2 に答える 2

16

Reflectionを使用すると、クラスが実装するClass.getInterfaces()を返すメソッドを呼び出すことができます。Array of Interfaces

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

名前だけを取得するには

list.getClass().getInterfaces()[0].getSimpleName();
于 2013-01-31T11:11:26.660 に答える
2
Class  aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces();
于 2013-01-31T11:12:35.350 に答える