您的位置:首页 > 编程语言 > Java开发

java 自定义注解

2016-04-07 17:33 351 查看
元注解:

  元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:

    1.@Target,

    2.@Retention,

    3.@Documented,

    4.@Inherited

  这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。

@Target:

   @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

  作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

  取值(ElementType)有:

    1.CONSTRUCTOR:用于描述构造器

    2.FIELD:用于描述域

    3.LOCAL_VARIABLE:用于描述局部变量

    4.METHOD:用于描述方法

    5.PACKAGE:用于描述包

    6.PARAMETER:用于描述参数

    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
实例:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
public String anthor();
public String descripte();

}


使用

@MyAnnotation(anthor="sunny",descripte="define annotation by myself")
public class Demo {
public static void main(String[] args) {
fun2();
}
public static void fun2(){
Class<Demo> c = Demo.class;
Annotation[] an = c.getAnnotations();

for(Annotation a : an){
if(a instanceof MyAnnotation){
MyAnnotation _a = (MyAnnotation)a;
System.out.println(_a.anthor());
System.out.println(_a.descripte());
}
}

}
public static void fun1(){
Class<Demo> c = Demo.class;
MyAnnotation my = (MyAnnotation)c.getAnnotation(MyAnnotation.class);
System.out.println(my.anthor());
System.out.println(my.descripte());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: