|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   yanqi_vip  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示