我延长在库中定义的类,我不能改变:使用原始类型参数覆盖方法时可以避免未经检查的警告吗?
public class Parent
{
public void init(Map properties) { ... }
}
如果我定义一个类“儿童”,扩展父母和我使用的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”)?
@SuppressWarnings绝对是您在这里最好的选择。吮吸,但是当你试图强制泛型进入不使用它的库时会发生这种情况。 – 2008-10-28 02:52:56