什么是Java中的SuppressWarnings(“未选中”)? [英] What is SuppressWarnings ("unchecked") in Java?

查看:122
本文介绍了什么是Java中的SuppressWarnings(“未选中”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候在查看代码时,我看到很多方法指定了注释:

  @SuppressWarnings(unchecked)

这是什么意思?

解决方案

有时Java泛型不会让你做你想做的事情,而且你需要有效地告诉编译器,你正在做的事情在执行时是合法的。

当我嘲笑泛型界面时,我通常会觉得这很痛苦,但也有其他例子。通常值得设法避免警告而不是压制警告( Java泛型常见问题解答在这里有帮助),但有时即使它 是可能的,它会使代码变形太多,以至于抑制警告更简洁。总是在这种情况下添加解释性评论!



相同的泛型常见问题解答有几个部分关于此主题,从什么是未经检查的警告? - 非常值得一读。


Sometime when looking through code, I see many methods specify an annotation:

@SuppressWarnings("unchecked")

What does this mean?

解决方案

Sometimes Java generics just doesn't let you do what you want to, and you need to effectively tell the compiler that what you're doing really will be legal at execution time.

I usually find this a pain when I'm mocking a generic interface, but there are other examples too. It's usually worth trying to work out a way of avoiding the warning rather than suppressing it (the Java Generics FAQ helps here) but sometimes even if it is possible, it bends the code out of shape so much that suppressing the warning is neater. Always add an explanatory comment in that case!

The same generics FAQ has several sections on this topic, starting with "What is an "unchecked" warning?" - it's well worth a read.

这篇关于什么是Java中的SuppressWarnings(“未选中”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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