@SuppressWarnings 有什么用 [英] What is the use of @SuppressWarnings

查看:44
本文介绍了@SuppressWarnings 有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白 Java 的这个特性.我知道它使编码更容易,有时看起来更整洁,但它的实际用途是什么?相反,我觉得最好显示警告,因为将来任何人都可以在修改代码之前参考它们.这个@SuppressWarnings 会提高编译效率还是根据任何编码标准?

解决方案

其他答案已经解释了很多 @SuppressWarnings 的用例,但我想强调一点,有时你绝对需要使用 @SuppressWarnings 来克服语言本身的限制,在这些情况下使用 @SuppressWarnings 是绝对合法的.

在其他情况下,@SuppressWarnings 的使用可能被认为是有问题的,因为在这些情况下,您总是可以通过更改代码来消除警告(尽管显然,这并不总是可以接受的).

如果没有@SuppressWarnings,您绝对无法摆脱警告,以下是一些常见情况:

  • 实现基于数组的泛型集合(因为您无法创建泛型数组)
  • 从类到它们的实现的Map(因为您不能将不同的泛型类型参数值分配给不同的映射整体)

I didn't understand this feature of Java. I know it makes coding easier and sometimes looks neater, but what is the actual use of this? On contrary i feel, its better to display the warnings, as in future any one can refer them before making modifications to code. Does this @SuppressWarnings increases compliling efficiency OR is this according to any coding standards?

解决方案

Other answers already explained use cases of @SuppressWarnings a lot, but I want to emphasize the point that sometimes you absolutely need to use @SuppressWarnings to overcome limitations of the language itself, and in these cases use of @SuppressWarnings is absolutely legal.

In other cases use of @SuppressWarnings can be considered questionable, because in these cases you can always get rid of warnings by changing the code (though, obviously, it's not always acceptable).

Here are some common cases when you absolutely cannot get rid of warnings without @SuppressWarnings:

  • Implementing an array-backed generic collection (since you cannot create a generic array)
  • A Map from classes to their implementations (since you cannot assign different values of generic type parameter to different map entires)

这篇关于@SuppressWarnings 有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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