首页 > 编程语言 > 详细

Java @SuppressWarnings

时间:2019-01-09 00:22:32      阅读:212      评论:0      收藏:0      [点我收藏+]

@SuppressWarnings() 注解以@开头可以接受参数

@SuppressWarnings("unchecked") 不受检查的警告信息应该被抑制

//: holding/ApplesAndOrangesWithoutGenerics.java
// Simple container example (produces compiler warnings).
// {ThrowsException}
package object;
import java.util.*;

class Apple {
  private static long counter;
  private final long id = counter++;
  public long id() { return id; }
}

class Orange {}    

public class ApplesAndOrangesWithoutGenerics {
  @SuppressWarnings("unchecked")
  public static void main(String[] args) {
    ArrayList apples = new ArrayList();
    for(int i = 0; i < 3; i++)
      apples.add(new Apple());
    // Not prevented from adding an Orange to apples:
    apples.add(new Orange());
    for(int i = 0; i < apples.size(); i++)
      ((Apple)apples.get(i)).id();//注释@SuppressWarnnigs("unchecked")抑制了类型转化错误信息
      // Orange is detected only at run time
  }
} /* (Execute to see output) *///:~

 

Java @SuppressWarnings

原文:https://www.cnblogs.com/jiangfeilong/p/10242083.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!