Java @SuppressWarnings

@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) *///:~

 

posted @ 2019-01-08 23:46  江期玉  阅读(376)  评论(0编辑  收藏  举报