更新时间:2022-06-19 08:26:14
不同的是,当你使用原始类型时,所有成员签名中的泛型引用也被转换为它们的原始形式.如此有效地调用了一个现在具有如下签名的方法:
The difference is that when you use the raw type, all the generic references within the member signatures are converted to their raw forms too. So effectively you're calling a method which now has a signature like this:
List getList()
现在至于为什么你的最终版本会编译——虽然它会编译,但如果你使用 -Xlint
会有一个警告:
Now as for why your final version compiles - although it does, there's a warning if you use -Xlint
:
Generics.java:16: warning: [unchecked] unchecked conversion
List<String> list = generics.getList();
^
这类似于:
List list = new ArrayList();
List<String> strings = list;
... 也可以编译,但在 -Xlint
下有警告.
... which also compiles, but with a warning under -Xlint
.
故事的寓意:不要使用原始类型!
The moral of the story: don't use raw types!