2
Since you haven't parameterized this new instance creation expression
由於您尚未參數化此新實例創建表達式
new MyClass()
The resulting value is of the raw type MyClass. The Java Language Specification states
結果值是原始類型MyClass。 Java語言規范說明
The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.
未從其超類或超接口繼承的原始類型C的構造函數(第8.8節),實例方法(第8.4節,第9.4節)或非靜態字段(第8.3節)的類型是對應於的原始類型在與C對應的泛型聲明中擦除其類型
That is
public ArrayList bla(){
return new ArrayList();
}
is now viewed as
現在被視為
public ArrayList bla() {
return new ArrayList();
}
The return type is itself the raw type ArrayList and the type of its add method also becomes the raw type of its erasure, ie. Object from generic variable T.
返回類型本身是原始類型ArrayList,其add方法的類型也成為其擦除的原始類型,即。來自通用變量T的對象
Obviously, you can still add a String to a method that expects an Object, but type safety is broken for anyone that has a properly parameterized view of the MyClass instance.
顯然,您仍然可以將String添加到需要Object的方法中,但對於具有MyClass實例的正確參數化視圖的任何人來說類型安全性都會被破壞。
Read: