java中returntype,java泛型方法返回類型[重復]

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值