简体   繁体   English

我应该使用问号类型参数还是禁止原始类型警告?

[英]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 type Class<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.

相关问题 警告:[rawtypes]找到原始类型:DefaultListModel - warning: [rawtypes] found raw type: DefaultListModel Java 6:不支持的@SuppressWarnings(“rawtypes”)警告 - Java 6: Unsupported @SuppressWarnings(“rawtypes”) warning Trating警告:[rawtypes]发现原始类型:Netbeans中的JList(更改对象类型以满足新的Java 7标准) - Trating warning: [rawtypes] found raw type: JList in Netbeans (changing the object types to meet the new Java 7 standards) 我使用@SuppressWarnings来抑制“可以私有”警告的价值是多少? - What value for @SuppressWarnings do I use to suppress the “Can be private” warning? 如何使用 Java Generic 来抑制警告? - How can I use Java Generic to suppress the warning? 我可以用什么黑客来抑制未使用的功能警告? - What hack can I use to suppress an unused function warning? rawtypes编译器警告通用数组创建 - rawtypes compiler warning on generic array creation 如何取消对注释参数的弃用警告 - How to suppress deprecation warning on annotation argument 如何在 Java 中使用带有问号的 strftime function - How can I use strftime function with question mark in Java 我应该如何修复代码,以便返回正确的输出,用&而不是问号? - How should I fix my code so I return the correct output, a & instead of a question mark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM