Java中的@SuppressWarnings是一个注解,用于通知编译器抑制程序某一部分的指定警告。 @SuppressWarnings 是一个内置注释,就像表示元数据的标签一样,它提供了附加信息。有时警告是好的,但有时它们会不合适且令人讨厌,那么程序员有时可以通知编译器抑制这样的警告。注意@SuppressWarnings中指定的编译器警告会抑制程序的某一部分;例如,如果一个方法被注释为抑制指定的警告,则编译器仅抑制该方法内的指定警告,但如果一个类被注释为抑制指定的警告,则编译器将抑制该类内的方法中的指定警告。 🎜>
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
@SuppressWarnings( "warningOption" )
参数
@SuppressWarnings 如何在 Java 中使用示例工作?
示例#1
代码:
//package demo; import java.util.ArrayList; import java.util.List; public class Main { @SuppressWarnings("unchecked") // also can be write as @SuppressWarnings(value = "unchecked") public static void main( String[] arg) { List fruits = new ArrayList(); // this causes unchecked warning fruits.add("Apple"); System.out.println( fruits); } }
输出:
说明:如上面的代码,@SuppressWarnings注解在main()方法上注解为@SuppressWarnings(“unchecked”),要抑制的警告是unchecked warning,现在里面的所有代码还应用了 main() 方法。所以现在编译器不会发出关于这一行“fruits.add(“Apple”)”的警告;因为它使用原始类型集合。如代码中所示,它使用 @SuppressWarnings 注释来不修复该警告。在上面的代码中,如果我们不使用 @SuppressWarnings 注解,那么在“fruits.add(“Apple”);”行
示例#2
代码:
//package demo; import javax.swing.JFrame; public class Demo { @SuppressWarnings("deprecation") // also can be write as @SuppressWarnings(value = "deprecation") public static void main( String[] arg) { JFrame dlog = new JFrame(); dlog.setTitle("This is demo"); // this generate the deprecated warning as it is a deprecated method // JDK version 1.7 replace it by setVisible() method dlog.show(); dlog.setSize(500, 500); // so use the new version method dlog.setVisible(true); } }
输出:
说明: 如上面的代码,@SuppressWarnings 注解在 main() 方法上被注解为 @SuppressWarnings(“deprecation”),要抑制的警告为 deprecation warning。所以现在编译器不会发出关于这一行“dlog.show();”的警告因为此方法在 swing API 中已被 setVisible() 方法弃用。如代码中所示,它使用 @SuppressWarnings 注释来不修复该警告。
示例 #3
代码:
//package demo; import javax.swing.JFrame; @SuppressWarnings("deprecation") public class Demo { // also can be write as @SuppressWarnings(value = "deprecation") public static void main( String[] arg) { JFrame dlog = new JFrame(); dlog.setTitle("This is demo"); // this generate the deprecated warning as it is a deprecated method // JDK version 1.7 replace it by setVisible() method dlog.show(); dlog.setSize(500, 500); // so use the new version method dlog.setVisible(true); } public void anotherMethod() { JFrame dlog = new JFrame(); dlog.setTitle("This is demo"); dlog.show(); } }
输出:
说明: 如上面的代码,@SuppressWarnings 注解被注释为 @SuppressWarnings(“deprecation”),这意味着现在类中的所有方法也将警告弃用应用到被抑制,现在编译器不会在两个方法行“dlog.show();”中发出警告。
如果我们在上面的代码中在 main() 方法级别使用 @SuppressWarnings 注解,则警告消息将显示在 anotherMethod() 方法中,如下图所示。示例#4
代码:
//package demo; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; public class Demo { @SuppressWarnings({"unchecked","deprecation"}) // also can be write as @SuppressWarnings(value={"unchecked","deprecation"}) public static void main( String[] arg) { JFrame dlog = new JFrame(); List title = new ArrayList(); // this causes unchecked warning title.add("This is demo"); dlog.setTitle(title.toString()); // this generate the deprecated warning as it is a deprecated method // JDK version 1.7 replace it by setVisible() method dlog.show(); dlog.setSize(500, 500); // so use the new version method dlog.setVisible(true); } }
输出:
说明: 如上面的代码,@SuppressWarnings 注解在 main() 方法上被注解为 @SuppressWarnings({“unchecked”, “deprecation”}),以抑制或忽略多个警告“unchecked”和“deprecation”,这意味着编译器不会发出与“unchecked”和“deprecation”相关的警告。
@SuppressWarning是Java中的一个注释,它通知编译器忽略对其注释的程序特定部分的指定警告。
以上是Java 中的@SuppressWarnings的详细内容。更多信息请关注PHP中文网其他相关文章!