【Java】@interface 元注解 实操详解 @Retention、@Target、@Documented 、@Inherited、@Repeatable

目录

元注解的含义

@Retention

@Target

@Documented

@Inherited

@Repeatable


@Retention        @Target        @Documented        @Inherited       @Repeatable


元注解的含义

        用来对注解的位置、作用范围、继承、文档抽取进行描述


@Retention

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

//  @Retention(RetentionPolicy.SOURCE) //  MyAnno注解在源代码中有效
//  @Retention(RetentionPolicy.CLASS) //  MyAnno注解在类文件中有效
@Retention(RetentionPolicy.RUNTIME) //   MyAnno注解在运行时有效

public @interface MyAnno {
    public String value() default "";
}
@MyAnno("我是一个注解")
public class AnnoTest {
    public static void main(String[] args) {
        AnnoTest  at= new AnnoTest();
        MyAnno annotate = at.getClass().getAnnotation(MyAnno.class);
        System.out.println(annotate.value());
    }
}

输出:我是一个注解

@Target

表示该注解可以给哪些元素使用

ElementType.ANNOTATION_TYPE:        注解

ElementType.CONSTRUCTOR:              构造方法

ElementType.FIELD:                                字段、枚举常量

ElementType.LOCAL_VARIABLE:           局部变量

ElementType.METHOD:                          方法

ElementType.PACKAGE:                         包

ElementType.PARAMETER:                   形参

ElementType.TYPE:                                类、接口、注解、枚举

ElementType.TYPE_PARAMETER:        类型参数

ElementType.TYPE_USE:                       类型使用 

@Target({ElementType.METHOD,ElementType.FIELD})
public @interface MyAnno {
    public String value() default "";
}

@Documented

注解信息可以被 javadoc 工具提取到API文档中

@Documented
public @interface MyAnno {
    public String value() default "";
}

IDE路径:Tools -- Generate JavaDoc


@Inherited

修饰的注解具有继承性

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
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String value() default "";
}
@MyAnnotation("父亲")
public class Father {}
public class Child extends Father{
	public static void main(String[] args) {
		Class<Child> child=Child.class;
		MyAnnotation anno = child.getAnnotation(MyAnnotation.class);
		System.out.println(anno.value());
	}
}
输出:父亲

@Repeatable

声明的注解可以重复注解在对应的元素上,可以通过另一个注解的值来包含这个可重复的注解。

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME) //
@Target(ElementType.TYPE)
public @interface MyAnnos {
    MyAnno[] value() ;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(MyAnnos.class)
public @interface MyAnno {
    String value() default "";
}
import com.prv.anno.MyAnno;

@MyAnno("anno")
@MyAnno("lala")
public class AnnoTest {
    public static void main(String[] args) {
        MyAnno[] fileTypes = new AnnoTest().getClass().getAnnotationsByType(MyAnno.class);
        for (MyAnno myAnno : fileTypes) {
            System.out.println(myAnno.value());
        }
    }
}
输出:
      anno
      lala

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑口罩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值