前言
2022年2月17日15:28:44
提示:以下是本篇文章正文内容,下面案例可供参考
一、 元注解
1、元注解的基本介绍
JDK 的元 Annotation 用于修饰其他 Annotation。
2、元注解的种类
①、@Retention 注解
说明
只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留多长时间, @Rentention 包含一个 RetentionPolicy
类型的成员变量, 使用 @Rentention 时必须为该 value 成员变量指定值:
@Retention 的三种值
- RetentionPolicy.SOURCE: 编译器使用后,直接丢弃这种策略的注释
- RetentionPolicy.CLASS: 编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解。 这是默认值
- RetentionPolicy.RUNTIME:编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注解. 程序可以通过反射获取该注解

②、@Target


③、@Documented


④、@Inherited 注解

二、自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//注解的参数 : 参数类型 + 参数名 ();
String name();
String[] arr();
}
7941

被折叠的 条评论
为什么被折叠?



