여기에 무슨 일이 일어나고 있으며 어떻게 이해하고 있습니까? 나는이 같은 D를 선언하는 경우 그런데이상한 무시 규칙이 어떻게 지울 수 있는지 설명 할 수 있습니까?
class C<T> {
T id(T x) {
return null;
}
}
class D extends C<String> {
public Integer id(Integer x) { // compiles! but T shall be String ???
return 7;
}
}
public static void main(String[] args) {
System.out.println(new D().id("7")); // null
System.out.println(new D().id(7)); // 7
}
는 컴파일이 제대로 사용했는지 확인할 것 이후 컴파일러는 Object
와 유형 매개 변수를 대체 Name clash: The method id(Object) of type D has the same erasure as id(T) of type C<T> but does not override it:
class D extends C<String> {
public Object id(Object x) { // compile error !
return 7;
}
}
Nitpick : C의 id 메소드가 OP의 예에서 공개되지 않았습니다. (여전히 +1) –