2013-01-31 17 views

Odpowiedz

9

Używając Reflection można wywołać metodę Class.getInterfaces(), która zwraca wartość Array of Interfaces, którą implementuje twoja klasa.

System.out.println(list.getClass().getInterfaces()[0]); 

Aby dostać się tylko nazwa

System.out.println(list.getClass().getInterfaces()[0].getSimpleName); 
+2

Można też rekursja na interfejsach udzielonych przez te interfejsy. – Guillaume

+0

@Guillaume dobry punkt, to byłaby kolejność implementacji klas, na przykład interfejs [0] byłby java.util.List, a interfejs [1] byłby java.util.RandomAccess – PermGenError

+0

A jeśli chcesz uzyskać interfejs Collection masz wywoływać getInterface() na interfejsach zwróconych przez pierwsze wywołanie. – Guillaume

2
Class aClass = ... //obtain Class object. 
Class[] interfaces = aClass.getInterfaces(); 
Powiązane problemy