元注解

  • 元注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明.
  • 这些类型和它们所支持的类在java.lang.annotation包中可以找到.( @Target , @Retention ,@Documented , @lnherited )
    • @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
    • @Retention:表示需要在什么级别保存该注释信息﹐用于描述注解的生命周期
      • (SOURCE<CLAss < RUNTIME)
    • @Document:说明该注解将被包含在javadoc中
    • @Inherited:说明子类可以继承父类中的该注解
package com.wang.annotation;


import java.lang.annotation.*;

//定义一个注解
//Target 表示我们的注解可以用在哪些地方
//测试元注解
@MyAnnotation
public class Test02 {
    public void test(){
        
    }
}
@Target(value = {ElementType.METHOD,ElementType.TYPE})

//Retention 表示我们的注解在什么地方还有效
//runtime>class>sources
@Retention(value= RetentionPolicy.RUNTIME)

//Documented 表示是否将我们的注解生成在JAVAdoc中
@Documented

//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{


}
上一篇:Jetpack之LiveData分析,Android-MVP模式详解


下一篇:对比学习(Contrastive Learning) (2)