Znalazłem interesujący problem; następujące klasy kompiluje:Generics w nadpisanych metodach
public class Test {
public static void main(String[] args) throws Exception {
A a = new A();
B b = new B();
foo(a);
foo(b);
}
private static void foo(A a) {
System.out.println("In A");
}
private static void foo(B b) {
System.out.println("In B");
}
private static class A {}
private static class B extends A {}
}
ale ten się nie powiedzie:
public class Test {
public static void main(String[] args) throws Exception {
A<String> a = new A<>();
B b = new B();
foo(a);
foo(b);
}
private static void foo(A<String> a) {
System.out.println("In A");
}
private static void foo(B b) {
System.out.println("In B");
}
private static class A<T> {}
private static class B extends A {}
}
z tego błędu:
Test.java:8: error: reference to foo is ambiguous, both method foo(A<String>) in Test and method foo(B) in Test match
foo(b);
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
bym nie pomyślał, że ze względu na typ skasowaniu, które są zasadniczo identyczny. Czy ktoś wie, co się tutaj dzieje?
Co to jest 'A a = nowy A <>();'? A może "A a = nowy A ();"? –
Trinimon
@ Trinimon To po prostu skrótowa notacja Java7 dla A a = nowa A (); –