使用原始类型参数重写方法时,是否可以避免未选中的警告?
2022-09-03 06:51:01
我正在扩展一个在库中定义的类,我无法更改:
public class Parent
{
public void init(Map properties) { ... }
}
如果我正在定义一个扩展 Parent 的类 'Child',并且我正在将 Java 6 与泛型一起使用,那么在不收到未经检查的警告的情况下重写 init 方法的最佳方法是什么?
public class Child extends Parent
{
// warning: Map is a raw type. References to generic type Map<K,V> should be parameterized
public void init(Map properties) { }
}
如果我添加通用参数,我得到:
// error: The method init(Map<Object,Object>) of type Child has the same erasure as init(Map) of type Parent but does not override it
public void init(Map<Object,Object>) { ... }
// same error
public void init(Map<? extends Object,? extends Object>) { ... }
// same error
public void init(Map<?,?>) { ... }
无论我是使用特定类型、有界通配符还是无界通配符,都会发生此错误。有没有一种正确或惯用的方法可以覆盖非泛型方法而不发出警告,也不使用@SuppressWarnings(“unchecked”)?