【发布时间】:2019-12-05 00:59:35
【问题描述】:
对于普通(非泛型)方法,要求子方法与父方法的擦除等效。
但是对于泛型方法,子方法需要有相同类型的参数,这意味着每个参数有相同的界限,例如:
public abstract class Parent {
public abstract <T> T example( Map<String, Integer> map) throws Exception;
}
public class Son1 extends Parent {
// this could work
@Override
public <T> T example(Map<String, Integer> map) throws Exception {
return null;
}
}
public class Son2 extends Parent {
// and this couldn't work
@Override
public <T> T example(Map map) throws Exception {
return null;
}
}
public class Son3 extends Parent {
// and this could work
@Override
public Object example(Map map) throws Exception {
return null;
}
}
我可以找到这项检查的完成地点(hasSameBounds)。
为什么 Son2 不工作? JLS中有描述吗?
【问题讨论】:
-
不,用参数类型为
Map的方法覆盖参数类型为Map<String, Integer>的方法始终是一个问题——它总是导致“原始类型”。原始类型只是为了向后兼容 Java 1.5 之前的代码。在public <T> T example(Map map)中的方法中将原始类型与泛型类型参数结合起来根本没有意义(因为原始类型仅存在于不理解泛型的代码中)并且被拒绝。 -
@ErwinBolwidt Son3的情况是为了向后兼容,在JLS中有描述。但我找不到为什么 Son2 不能工作,而 Son1 可以在 JLS 中工作。这就是问题所在。