学习Java注解Annotation

转:http://tomcat-oracle.iteye.com/blog/1964381

1.概述

    注解可以定义到方法上,类上,一个注解相当与一个类,就相当于实例了一个对象,加上了注解,就相当于加了一个标志。

 

    常用的注解:

 

    @Override:表示重新父类的方法,

 

    这个也可以判断是否覆盖的父类方法,在方法前面加上此语句,如果提示的错误,那么你不是覆盖的父类的方法,要是提示的没有错误,那么就是覆盖的父类的方法。

 

    @SuppressWarnings("deprecation"):取消编译器的警告(例如你使用的方法过时了)

 

    @Deprecated:在方法的最上边也上此语句,表示此方法过时,了,或者使用在类上面

 

Java代码   收藏代码
  1. import java.util.ArrayList;  
  2.     import java.util.List;  
  3.     public class annotationDemo {  
  4.     /* 
  5.     * 对于集合,如果没有指定存储的类型,那么就会有安全警告, 
  6.     * 如果不想提示安全警告的话,那么就所在类或者方法上添加@SuppressWarnings(参数) 
  7.     */  
  8.     @SuppressWarnings"unchecked")  
  9.     public static void main(String[] args) {  
  10.     List list=new ArrayList();  
  11.     }  
  12.     }  

 

 2.自定义注解

 

    1.格式

 

    权限 @interface 注解名称 { }

 

    步骤:

 

    定义注解类--->定义应用注解类的类--->对应用注解类的类进行反射的类(这个类可以另外定义,也可以是在应用注解类中进行测试

 

Java代码   收藏代码
  1. import java.lang.annotation.Retention;  
  2.     importjava.lang.annotation.RetentionPolicy;  
  3.     //定义此注解保留在字节码中  
  4.     @Retention(RetentionPolicy.RUNTIME)  
  5.     public @interface MyAnnotation {  
  6.     }  
  7.     @MyAnnotation  
  8.     // 应用定义的注解类  
  9.     public class ApplyMyAnnotation {  
  10.     public static void main(String[] args) {  
  11.     if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类  
  12.     MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class  
  13.     .getAnnotation(MyAnnotation.class);  
  14.     System.out.println(annotation);  
  15.     }  
  16.    }  
  17. }  

 

 2.声明周期

 

    格式:例如:@Retention(RetentionPolicy.CLASS)

 

    在自定一的注解类上定义周期,@Retention(参数类型) 参数类型是RetentionPolicy

 

    RetentionPolicy.CLASS:类文件上,运行时虚拟机不保留注解

 

    RetentionPolicy.RUNTIME:类文件上,运行时虚拟就保留注解

 

    RetentionPolicy.SOURCE:源文件上,丢弃注解

 

    SuppressWarnings和Override是RetentionPolicy.SOURCE,

 

    Deprecated是在RetentionPolicy.RUNTIME,要向运行时调用定义的一样,那么必须是RetentionPolicy.RUNTIME,

 

    默认的都是RetentionPolicy.CLASS:

 

3.指定目标

 

    格式:例如:方法上@Target(ElementType.METHOD)

 

    定义的注解可以注解什么成员。如果不声明此注解,那么就是可以放到任何程序的元素上。

 

    可以是包,接口,参数,方法,局部变量,字段…等。

 

Java代码   收藏代码
  1. //定义此注解保留在字节码中  
  2.     @Retention(RetentionPolicy.RUNTIME)  
  3.     @Target({ElementType.METHOD,ElementType.TYPE})//可以定义在方法上和类上接口,表示类型  
  4.     public @interface MyAnnotation {  
  5.     }  
  6.     @MyAnnotation  
  7.     // 应用定义的注解类  
  8.     public class ApplyMyAnnotation {  
  9.     @MyAnnotation//定义在方法上  
  10.     public static void main(String[] args) {  
  11.     if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类  
  12.     MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class  
  13.     .getAnnotation(MyAnnotation.class);  
  14.     System.out.println(annotation);  
  15.     }  
  16.     }  
  17.     }  

 

  3.为注解添加属性

 

    1.类型

 

    注解的属性置可以是:8个基本数据类型,String,枚举,注解,Class,数组类型,

 

    2.注意点

 

    当注 解中只有一个属性或者是只有一个属性需要赋值的话,那么在调用的时候,就可以直接写入,不需要指定属性名,

 

    当注解的属性是数组类型并且赋值的时候只赋值一个值,那么就可以省略{}.

 

    3.示例

 

    3.1.属性类型(是String)

 

Java代码   收藏代码
  1. import java.lang.annotation.ElementType;  
  2.     import java.lang.annotation.Retention;  
  3.     import java.lang.annotation.RetentionPolicy;  
  4.     import java.lang.annotation.*;  
  5.     //定义此注解保留在字节码中  
  6.     @Retention(RetentionPolicy.RUNTIME)  
  7.     public @interface MyAnnotation {  
  8.     String value() ;  
  9.     String Color()default "red";//设置默认值是"red"  
  10.     }  
  11.     @MyAnnotation"java")  
  12.     public class ApplyMyAnnotation {  
  13.     public static void main(String[] args) {  
  14.     /** 
  15.     * 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例 
  16.     */  
  17.     if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类  
  18.     MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class  
  19.     .getAnnotation(MyAnnotation.class);  
  20.     System.out.println("value="+annotation.value());  
  21.     System.out.println("Color="+annotation.Color());  
  22.     }  
  23.     }  
  24.   }  

 

 结果:

 

    value=java

 

    Color=red

 

    从调用的程序中,也可以看出,只有一个属性可以需要赋值的话,可以省略属性名。否则@注解类(属性名=值)

 

    3.2.综合类型

 

Java代码   收藏代码
  1. /*枚举类*/  
  2.     public enum Week{  
  3.     SUN,MON;  
  4.     }  
  5.     /** 
  6.     * 注解类 
  7.     */  
  8.     public @interface annotationText {  
  9.     String value();  
  10.     }  
  11.     import java.lang.annotation.ElementType;  
  12.     import java.lang.annotation.Retention;  
  13.     import java.lang.annotation.RetentionPolicy;  
  14.     import java.lang.annotation.*;  
  15.     //定义此注解保留在字节码中  
  16.     @Retention(RetentionPolicy.RUNTIME)  
  17.     public @interface MyAnnotation {  
  18.     String value() ;  
  19.     String Color()default "red";//设置默认值是"red"  
  20.     Week week() default Week.MON;//枚举类型  
  21.     int [] array() default {1,2,3};//数组类型  
  22.     annotationText annotation() default @annotationText"MY");//注解类型  
  23.     Class classDemo() default Integer.class;//Class类型  
  24.     }  
  25.     @MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText"YOU"),classDemo=String.class//数组array={4,5,6}  
  26.     public class ApplyMyAnnotation {  
  27.     public static void main(String[] args) {  
  28.     /** 
  29.     * 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例 
  30.     */  
  31.     if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类  
  32.     MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class  
  33.     .getAnnotation(MyAnnotation.class);  
  34.     System.out.println("value="+annotation.value());  
  35.     System.out.println("Color="+annotation.Color());  
  36.     System.out.println("week="+annotation.week());  
  37.     System.out.println("array长度="+annotation.array()。length);  
  38.     System.out.println("注解类型值="+annotation.annotation()。value());  
  39.     System.out.println("Class类型值="+annotation.classDemo());  
  40.     }  
  41.     }  
  42.     }  

 

 结果:

 

    value=java

 

    Color=green

 

    week=SUN

 

    array长度=1

 

    注解类型值=YOU

 

    Class类型值=classjava.lang.String

 

    4.Method上的注解

 

Java代码   收藏代码
  1. import java.lang.annotation.Retention;  
  2.    import java.lang.annotation.RetentionPolicy;  
  3.    /** 
  4.    * 注解类 
  5.    */  
  6.    @Retention(RetentionPolicy.RUNTIME)  
  7.    public @interface annotationText {  
  8.    String value();  
  9.    }  
  10.    public class ApplyMyAnnotation {  
  11.    public static void main(String[] args) throws Exception {  
  12.    Method methodshow = ApplyMyAnnotation.class.getMethod("show");  
  13.    annotationText anno = methodshow.getAnnotation(annotationText.class);  
  14.    System.out.println(anno.value());  
  15.    }  
  16.    @annotationText"java")  
  17.    public void show() {  
  18.    System.out.println("hello");  
  19.    }  
  20.    }  

 

 结果:

 

    java

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值