Java 注解

来源:互联网 发布:树莓派可以装windows吗 编辑:程序博客网 时间:2024/04/29 16:46

1、概念
An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate.
能够添加到 Java 源代码的语法元数据。类、方法、变量、参数、包都可以被注解,可用来将信息元数据与程序元素进行关联。Annotation 中文常译为“注解”。

就是对某一事物进行添加注释说明,会存放一些信息,这些信息可能对以后某个时段来说是很有用处的。 

Java注解又叫java标注,java提供了一套机制,使得我们可以对方法、类、参数、包、域以及变量等添加标准(即附上某些信息)。且在以后某个时段通过反射将标注的信息提取出来以供使用。 

2,语法

注解的语法比较简单,除了@符号的使用之外,它基本与Java固有语法一致。在我看来跟接口很像。Java SE5内置了三种标准注解:

@Override,表示当前的方法定义将覆盖超类中的方法。

     @Deprecated,使用了注解为它的元素编译器将发出警告,因为注解@Deprecated是不赞成使用的代码,被弃用的代码。

     @SuppressWarnings,关闭不当编译器警告信息。

上面这三个注解多少我们都会在写代码的时候遇到。Java还提供了4中注解,专门负责新注解的创建。

 

@Target

表示该注解可以用于什么地方,可能的ElementType参数有:

CONSTRUCTOR:构造器的声明

FIELD:域声明(包括enum实例)

LOCAL_VARIABLE:局部变量声明

METHOD:方法声明

PACKAGE:包声明

PARAMETER:参数声明

TYPE:类、接口(包括注解类型)或enum声明

@Retention

表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:

SOURCE:注解将被编译器丢弃

CLASS:注解在class文件中可用,但会被VM丢弃

RUNTIME:VM将在运行期间保留注解,因此可以通过反射机制读取注解的信息。

@Document

将注解包含在Javadoc中

@Inherited

允许子类继承父类中的注解



3,应用

1)、定义一个最简单的注解
public @interface MyAnnotation {
    //......
}
2)、把注解加在某个类上:
@MyAnnotation
public class AnnotationTest{
    //......
}
以下为模拟案例
自定义注解@MyAnnotation
package com.ljq.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 定义一个注解 *  *  * @author jiqinlin * */ //Java中提供了四种元注解,专门负责注解其他的注解,分别如下 //@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括: //RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉 //RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认) //RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息 //@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括 //ElementType.CONSTRUCTOR: 构造器声明 //ElementType.FIELD: 成员变量、对象、属性(包括enum实例) //ElementType.LOCAL_VARIABLE: 局部变量声明 //ElementType.METHOD: 方法声明 //ElementType.PACKAGE: 包声明 //ElementType.PARAMETER: 参数声明 //ElementType.TYPE: 类、接口(包括注解类型)或enum声明 //@Documented将注解包含在JavaDoc中 //@Inheried允许子类继承父类中的注解 @Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE}) public @interface MyAnnotation {    //为注解添加属性     String color();    String value() default "我是林计钦"; //为属性提供默认值     int[] array() default {1, 2, 3};     Gender gender() default Gender.MAN; //添加一个枚举     MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期为1988-2-18");    //添加枚举属性 }

注解测试类AnnotationTest

package com.ljq.test; /** * 注解测试类 *  *  * @author jiqinlin * */ //调用注解并赋值 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期为1988-2-18"),color="red", array={23, 26}) public class AnnotationTest {    public static void main(String[] args) {        //检查类AnnotationTest是否含有@MyAnnotation注解         if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){            //若存在就获取注解             MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);            System.out.println(annotation);            //获取注解属性             System.out.println(annotation.color());             System.out.println(annotation.value());            //数组             int[] arrs=annotation.array();            for(int arr:arrs){                System.out.println(arr);            }            //枚举             Gender gender=annotation.gender();            System.out.println("性别为:"+gender);            //获取注解属性             MetaAnnotation meta=annotation.metaAnnotation();            System.out.println(meta.birthday());        }    }}

枚举类Gender,模拟注解中添加枚举属性

package com.ljq.test; /** * 枚举,模拟注解中添加枚举属性 *  * @author jiqinlin * */ public enum Gender {    MAN{        public String getName(){return "男";}    },    WOMEN{        public String getName(){return "女";}    }; //记得有“;”     public abstract String getName();}

注解类MetaAnnotation,模拟注解中添加注解属性

package com.ljq.test; /** * 定义一个注解,模拟注解中添加注解属性 *  * @author jiqinlin * */ public @interface MetaAnnotation {    String birthday();}


0 0