Annotation

@interface:这个关键字隐含的意思就是继承了java.lang.annotation.Annotation接口。

注解的定义

 1 未包含成员
 2 public @interface testAnnotaion{
 3 }
 4 
 5 包含一个成员
 6 public @interface testAnnotaion{
 7     String value();
 8 }
 9 
10 包含多个成员
11 public @interface testAnnotaion{
12     String describe();
13     Class type();
14 }
15 
16 为成员设置默认值
17 public @interface testAnnotation{
18     String describe() default "<默认值>";
19     Class type() default void.class;
20 }

元注解:注解到注解上的注解,或者说元注解是一种基本的注解。

 1 元注解有5中
 2 @Retention 保存,保留物(解释说明注解的存活时间)
 3     RententionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它就被丢弃忽视
 4     RententionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到JVM中
 5     RententionPolicy.RUNTIME 注解可以保留到程序运行的时候,它会被加载到JVM中,所以在程序运行时可以获取到他们
 6 @Target 目标(指定了注解运用的地方)
 7     ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
 8     ElementType.CONSTRUCTOR 可以给构造方法进行注解
 9     ElementType.FIELD 可以给属性进行注解
10     ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
11     ElementType.METHOD 可以给方法进行注解
12     ElementType.PACKAGE 可以给一个包进行注解
13     ElementType.PARAMETER 可以给一个方法内的参数进行注解
14     ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
15 @Documented 
16 @Inherited
17 @Repeatable

 

posted @ 2020-05-31 09:34  wyl677  阅读(104)  评论(0)    收藏  举报