注解的注解--MyAnnotation2

来源:互联网 发布:mac词典设置 编辑:程序博客网 时间:2024/06/06 00:23

注解的注解:MyAnnotation2

/**
 * 对注解的注解:
 * 1、指定目标@Target:(ElementType是一个枚举类)
 *   --若不定义@Target,则注解可以放在任意位置,如:类、变量、构造器、方法等
 *     通过ElementType类中定义的许多常量,来限制MyAnnotation注解的位置,
 *     如:ElementType.FIELD(成员变量)、ElementType.METHOD(成员方法)等,详情见API中ElementType类
 *     
 * 2、设置保持性@Retention:(RetentionPolicy类有3个枚举常量)--"CLASS是默认的"
 *   --1)CLASS--编译器将把"注解"记录在".class"文件中,但在运行时 VM(虚拟机)不需要保留"注解"--这个是默认的
 *   --2)RUNTIME--编译器将把"注解"记录在".class"文件中,在运行时 VM(虚拟机)将保留"注解",因此可以反射性地读取
 *   --3)SOURCE--编译器要丢弃的"注解"
 *   
 * 3、添加公共文档@Documented
 *   --在默认的情况下在使用javadoc自动生成文档时,注解将被忽略掉,
 *     如果想在文档中也包含注解,必须使用@Documented为文档注解
 *   
 * 4、设置继承 @Inherited
 *   --在默认的情况下,父类的注解并不会被子类继承,
 *     如果要继承,就必须加上Inherited注解
 */


package cn.hncu.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Inherited@Documented@Retention(RetentionPolicy.RUNTIME)//记录在.class文件中,并且在运行时保留"注释"@Target({ElementType.FIELD,ElementType.METHOD})//指定注解只能放在"变量"和"方法"上public @interface MyAnnotation2 {public String value() default "默认值...";}

0 0
原创粉丝点击