原始类型,无界wilcard和有界通配符 [英] Raw types, unbounded wilcard and bounded wildcard

查看:241
本文介绍了原始类型,无界wilcard和有界通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,如下所示:
下面是关于整个问题的简单示例:

 列出a = new ArrayList(); 
列表<?> b;
列表< ;?扩展对象> C;

根据khalid mughal的Java SCJP(一本非常好的书!):

  a = b; // 好。扩大转化。 
b = a; //也行。没有未经检查的警告。

b = c; //好的
c = b; //好的

c = a; //确定,但现在会发出未经检查的警告。 //条款1

我知道任何原始类型(示例a)在分配给任何有界wilcard引用,未经检查的警告是问题(因为原始类型a中的内容可能是任何东西)。



我的问题是因为c是最高的上限(?extends objects ),如果我没有正确理解你的问题(和我),应该不能被分配给C没有这种警告?

真的不认为我这样做),根据本页



  • 如果擦除更改了方法或构造函数的任何参数类型,则对原始类型的方法或构造函数的调用会生成未经检查的警告。

  • 如果擦除更改字段的类型,则对原始类型字段的分配会生成未经检查的警告(第5.1.9节)。

因此,您的问题的答案基本上似乎是擦除可能会导致未经检查的原始存在类型的警告。据我所知,当嵌套类型被使用时,这很可能发生 - 在擦除的定义中,我看不到其他任何可能导致类型改变的擦除定义,但也许其他人可以建议是否是或是不是这个来源。


I have a quick question as below: Here's a simple examples about this whole issues:

List a = new ArrayList();
List <?> b;
List <? extends Object> c;

According to Java SCJP by khalid mughal (a very good book!):

a = b; // ok. Widening conversion.
b = a; // ok too. No unchecked warning.

b = c; // ok
c = b; // ok

c=a; // ok but now will issue a unchecked warning. // clause 1

I do understand that any raw types (example a) when assigned to any bounded wilcard references, a unchecked warning is issues (since the content in that raw type a could be anything).

My questions is since c is the highest upper bound (? extends objects), shouldn't a be able to assigned to c without that warning?

解决方案

If I understand your question correctly (and I really don't think I do), there appear to be two instances in which interaction with raw types could cause an unchecked warning to occur, according to this page:

  • An invocation of a method or constructor of a raw type generates an unchecked warning if erasure changes any of the types of any of the arguments to the method or constructor.
  • An assignment to a field of a raw type generates an unchecked warning (§5.1.9) if erasure changes the field's type.

So the answer to your question basically seems to be "erasure could result in unchecked warnings in the presence of raw types". As far as I can tell, this is most likely to occur when nested types are used - I can't see anywhere else in the definition of erasure that would likely cause a type to change, but perhaps someone else can suggest whether that is or isn't the source of this.

这篇关于原始类型,无界wilcard和有界通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆