[英]Should I use question mark type argument or suppress rawtypes warning?
Since the introduction of generic type parameters (or type arguments) in Java, writing the following line in Eclipse IDE will show a yellow squiggly line on the type Class
: 由于Java中引入了泛型类型参数(或类型实参),因此在Eclipse IDE中编写以下行将在
Class
上显示一条黄色的弯曲行:
Class myClass;
The warning shown on a mouseover is the following, with the two options (among others): 鼠标悬停上显示的警告如下,其中包括两个选项:
Class
is a raw type.Class
是原始类型。 References to the generic typeClass<T>
should be parameterized.泛型类型
Class<T>
引用应参数化。
- Add type arguments to '
Class
'将类型参数添加到“
Class
”- Add
@SuppressWarnings
'rawtypes
' to 'myClass
'将
@SuppressWarnings
rawtypes
'添加到'myClass
'
The first option produces this code: 第一个选项产生以下代码:
Class<?> myClass;
The second one produces this: 第二个产生这个:
@SuppressWarnings("rawtypes")
Class myClass;
Both of the above options are equally adequate in taking care of the warning. 以上两种选择均足以应付警告。
Assuming neither we nor Eclipse can infer generic type arguments*; 假设我们和Eclipse都无法推断出通用类型参数*; what is the better approach to take, in what situations, and why?
什么是更好的方法?在什么情况下?为什么?
* (despite having the option to try to do so) *(尽管可以选择尝试这样做)
A general rule every programmer should adhere to: never suppress warnings for no good reason. 每个程序员都应遵守的一条一般规则:永远不要无缘无故地抑制警告。 If you suppress a warning, you should know why the warning occurs and why it does not present a problem in your case.
如果您取消警告,则应该知道为什么会发生该警告以及为什么它不会对您造成问题。
In this case the reason to suppress a rawtype warning would be to support legacy code that was written in a time when genericity did not exist in Java (See the Java Language Specification 4.8): 在这种情况下,取消原始类型警告的原因是要支持在Java中不存在通用性时编写的遗留代码(请参见Java语言规范4.8):
The use of raw types is allowed only as a concession to compatibility of legacy code.
仅允许使用原始类型作为对遗留代码兼容性的让步。 The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged.
强烈建议不要在将通用性引入Java编程语言之后在编写的代码中使用原始类型。 It is possible that future versions of the Java programming language will disallow the use of raw types.
Java编程语言的未来版本可能会禁止使用原始类型。
So it does not apply to your case, you should not suppress this warning. 因此,它不适用于您的情况,您不应抑制此警告。
As to the Wildcard ?
至于通配符
?
, this should only be used if you want to assign various types to that variable. ,仅当您想为该变量分配各种类型时才应使用。
For example a List<?> mylist
lets you store any type in the list, while List<? extends Collection>
例如,
List<?> mylist
可让您在列表中存储任何类型,而List<? extends Collection>
List<? extends Collection>
lets you store any type that inherits from Collection
to be stored in the list. List<? extends Collection>
允许您存储从Collection
继承的任何类型以存储在列表中。 A general rule is to be as specific about allowed types as possible. 一般规则是尽可能详细地说明允许的类型。 This way the compiler can notify you sonner if you accidentally add an object into a List that is not meant for it.
这样,如果您不小心将对象添加到了不适合它的列表中,则编译器可以通知您Sonner。
So the best way to go here is to think about how you want to use your myClass variable and if you can make a statement about which type of objects it will hold, replace the Wildcard with that type. 因此,最好的方法是考虑如何使用myClass变量,如果可以声明将保留哪种对象,请用该类型替换通配符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.