J2SE 5.0 提供的简单的批注类型

J2SE 5.0 提供的简单的批注类型

 

 

     1 @Override    2   @Deprecated   3 @SuppressWarnings

            1 @Override

       @Override 使您能够在代码中增加新的可选的编译器检查。它在方法中存在表示该方法用于覆盖父类中的方法。如果编译器检测到该方法实际上没有覆盖任何东西,那么将出现编译错误。经常使用,@Override 可以帮助您避免当方法标记没有完全匹配时当多态变为(您可以称之为)单态” ("unimorphism") 将得到的细微的 bug
     只有当您是一个愿意用 @Override 来标记每一个覆盖方法的非常严谨的编程人员时才有用。

           2   @Deprecated

   你可以这样使用它:

          public class DeprecatedExample {
              @Deprecated
            public static void badMethod() {
              }
           }

          public class DeprecatedUser {
               public static void main(String[] args) {
                DeprecatedExample.badMethod();
              }
          }

           3 @SuppressWarnings

     该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。

项目组来了个很Picky的头,看到Eclipse里有大量的Warning小发了下脾气后,

 让用@SuppressWarnings这个标注来把Warning去掉.

       

        为了去掉由于泛型的Warning,我们用了@SuppressWarnings("unchecked"),

         为了去掉那些由于deprecation而引起的Warning,用了@SuppressWarnings("deprecation").

         但一个类中既有unchecked又有deprecation,怎么办?于是用了一个很权宜之计:

         在类声明那用@SuppressWarnings("deprecation"),而在相应的方法声明里用@SuppressWarnings("unchecked").

 

       用了效果当然也达到了,可就想问在一个单独的@SuppressWarnings里既去掉deprecation又去掉unchecked?

       觉得应该可以达到的.于是就用下面的方式来试.

 

        1,@SuppressWarnings("deprecation","unchecked"),不行,Eclipse报错.

 

         2,@SuppressWarnings("deprecation,unchecked"),不行,还是报错.

         3,@SuppressWarnings的源码,只有一个String[]类型的value.就想用

          @SuppressWarnings(new String[]{"deprecation","unchecked"}),还是不行,报错.

 

         4, Google了下后,发现了这个@SuppressWarnings(value={"deprecation"}),

          于是就想起来了@SuppressWarnings(value={"deprecation","unchecked"}).呵呵,成了.

       

        好久没用接触过标注了,快忘了. 现在记下来

你可能感兴趣的:(eclipse,编程,Google,J2SE)