|NO.Z.00087|——————————|BigDataEnd|——|Java&特殊类.V15|——|Java.v15|元注解概念|@Retention使用|

一、元注解的概念和@Retention的使用
### --- 元注解的概念

~~~     ——>        元注解是可以注解到注解上的注解,或者说元注解是一种基本注解,
~~~     ——>        但是它能够应用到其它的注解上面。
~~~     ——>        元注解主要有@Retention、@Documented、@Target、@Inherited、@Repeatable。
### --- 元注解@Retention

~~~     ——>        @Retention 应用到一个注解上用于说明该注解的的生命周期,取值如下:
~~~     ——>        RetentionPolicy.SOURCE 注解只在源码阶段保留,
~~~     ——>        在编译器进行编译时它将被丢弃忽视。
~~~     ——>        RetentionPolicy.CLASS 注解只被保留到编译进行的时候,
~~~     ——>        它并不会被加载到JVM 中,默认方式。
~~~     ——>        RetentionPolicy.RUNTIME 注解可以保留到程序运行的时候,
~~~     ——>        它会被加载进入到JVM 中,所以在程序运行时可以获取到它们。
二、编程代码
package com.yanqi.task10;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

//@Retention(RetentionPolicy.SOURCE)     // 表示下面的注解在源代码中有效
//@Retention(RetentionPolicy.CLASS)      // 表示下面的注解在字节码文件中有效,默认方式
@Retention(RetentionPolicy.RUNTIME)      // 表示下面的注解在运行时有效
// 若一个注解中没有任何的成员,则这样的注解叫做标记注解/标识注解
public @interface MyAnnotation {
    //public Direction value(); // 声明一个String类型的成员变量,名字为value   类型有要求
    public String value() default "123"; // 声明一个String类型的成员变量,名字为value
    public String value2();
}
三、编程代码
package com.yanqi.task10;

// 表示将标签MyAnnotation贴在Person类的代码中,使用注解时采用 成员参数名 = 成员参数值, ...
//@MyAnnotation(value = "hello", value2 = "world")
@MyAnnotation(value2 = "world")
public class Person {
    private String name;
    private int age;
    }

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-03 18:11  yanqi_vip  阅读(27)  评论(0)    收藏  举报

导航