分享

Java零基础@Override @Deprecated @SuppressWarnings诠释

hyj 发表于 2014-7-7 00:40:39 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 9311
问题导读:

1.Java内置了哪三种注解?
2.如何定义注解实例?
3.如何通过注解关闭警告?






注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后
某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据)。

    注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
      @Override  表示当前方法是覆盖父类的方法。
      @Deprecated  表示这个方法或类不再建议使用。在新版本中有其他方法或类可以代替这个使用,以后的版本也不会再更新这个方法或类
      @SuppressWarnings 表示关闭一些不当的编译器警告信息。

  下面是一个定义注解的实例
  1. package Test_annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.Inherited;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7. import java.lang.annotation.ElementType;  
  8. import java.lang.annotation.RetentionPolicy;  
  9.   
  10. /*
  11. * 元注解@Target,@Retention,@Documented,@Inherited
  12. *  
  13. *     @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
  14. *         ElemenetType.CONSTRUCTOR 构造器声明
  15. *         ElemenetType.FIELD 域声明(包括 enum 实例)
  16. *         ElemenetType.LOCAL_VARIABLE 局部变量声明
  17. *         ElemenetType.METHOD 方法声明
  18. *         ElemenetType.PACKAGE 包声明
  19. *         ElemenetType.PARAMETER 参数声明
  20. *         ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
  21. *         
  22. *     @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
  23. *         RetentionPolicy.SOURCE 注解将被编译器丢弃
  24. *         RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
  25. *         RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
  26. *         
  27. *     @Documented 将此注解包含在 javadoc 中
  28. *      
  29. *     @Inherited 允许子类继承父类中的注解
  30. *   
  31. */  
  32. @Target(ElementType.METHOD)  
  33. @Retention(RetentionPolicy.RUNTIME)  
  34. @Documented  
  35. @Inherited  
  36. /*
  37. * 定义注解 Test
  38. * 注解中含有两个元素 id 和 description
  39. * description 元素 有默认值 "no description"
  40. */  
  41. public @interface Test {  
  42.     public int id();  
  43.     public String description() default "no description";  
  44. }  
复制代码

下面是一个使用注解 和 解析注解的实例

  1. package Test_annotation;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. public class Test_1 {  
  6.     /*
  7.      * 被注解的三个方法
  8.      */  
  9.     @Test(id = 1, description = "hello method_1")  
  10.     public void method_1() {  
  11.     }  
  12.   
  13.     @Test(id = 2)  
  14.     public void method_2() {  
  15.     }  
  16.   
  17.     @Test(id = 3, description = "last method")  
  18.     public void method_3() {  
  19.     }  
  20.   
  21.     /*
  22.      * 解析注解,将Test_1类 所有被注解方法 的信息打印出来
  23.      */  
  24.     public static void main(String[] args) {  
  25.         Method[] methods = Test_1.class.getDeclaredMethods();  
  26.         for (Method method : methods) {  
  27.             /*
  28.              * 判断方法中是否有指定注解类型的注解
  29.              */  
  30.             boolean hasAnnotation = method.isAnnotationPresent(Test.class);  
  31.             if (hasAnnotation) {  
  32.                 /*
  33.                  * 根据注解类型返回方法的指定类型注解
  34.                  */  
  35.                 Test annotation = method.getAnnotation(Test.class);  
  36.                 System.out.println("Test( method = " + method.getName()  
  37.                         + " , id = " + annotation.id() + " , description = "  
  38.                         + annotation.description() + " )");  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43. }  
复制代码


输出结果如下:

    Test( method = method_1 , id = 1 , description = hello method_1 )
    Test( method = method_2 , id = 2 , description = no description )
    Test( method = method_3 , id = 3 , description = last method )

另外提供一pdf文档,感觉不错
java注解详解.pdf (173.27 KB, 下载次数: 11)

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条