我延長在庫中定義的類,我不能改變:使用原始類型參數覆蓋方法時可以避免未經檢查的警告嗎?
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