當實現具有泛型返回類型的方法並反映實現類中聲明的方法時,反射API將返回兩個方法。例如:實現具有通用返回類型的方法時通過反射的多種方法
interface Referenceable<T> {
T getReference();
}
class UUIDRef implements Referenceable<UUID> {
public UUID getReference() {
System.out.println("You called");
return null;
}
}
@Test
public void test() throws Exception {
UUIDRef ref = new UUIDRef();
for (Method m : UUIDRef.class.getDeclaredMethods()) {
System.out.println(String.format("%s %s", m.getReturnType(), m.getName()));
m.invoke(ref, null);
}
}
此輸出:
class java.util.UUID getReference You called class java.lang.Object getReference You called
爲什麼會出現兩種方法已經被宣佈UUIDRef
?有沒有什麼方法可以準確地瞭解哪兩個是最精煉的,實際上是在UUIDRef
上宣佈的?
您是否嘗試將@Override註釋添加到實現的方法中? – ra2085
我實際上正在編譯1.5,@Override不允許在這裏。 – Jonathan