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

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

问题描述

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

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

@SuppressWarnings("unchecked")

这是什么意思?

推荐答案

有时 Java 泛型不能让你做你想做的事,你需要有效地告诉编译器你在做什么在执行时合法.

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.

当我模拟通用接口时,我通常会觉得这很痛苦,但也有其他示例.通常值得尝试找出一种避免警告而不是抑制警告的方法(Java泛型常见问题解答 在这里有帮助)但有时即使 可能,它也会使代码变形太多,以至于抑制警告更整洁.在这种情况下,请务必添加解释性注释!

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(“unchecked")是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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