【Java编程思想笔记】注解--元注解

参考文章:(小白的小小白的白 )https://blog.csdn.net/weixin_42315600/article/details/80630669

                  https://www.cnblogs.com/skywang12345/p/3344137.html

                  学习网站:how2java.cn

一、元注解概念:

        元注解的作用就是负责注解其他(如:自定义)注解,用来对其它 annotation类型作说明。

        元注解是自定义注解的重要组成部分,其可以很好地描述自定义注解的信息。

二、元注解种类:

         1、@Target : 说明了Annotation所修饰的对象范围作;用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

                              如其修饰的这个注解是只能放在类上,还是可以放在方法上,或者放在属性上

               如@Target({ElementType.METHOD,ElementType.TYPE})就表示,这个注解可以放在方法和类型上(类和接口),但是不能放在属性或别的位置。

             (1)ElementType.TYPE:能修饰类、接口或枚举类型

             (2)ElementType.FIELD:能修饰成员变量

             (3)ElementType.METHOD:能修饰方法

             (4)ElementType.PARAMETER:能修饰参数

             (5)ElementType.CONSTRUCTOR:能修饰构造器

             (6)ElementType.LOCAL_VARIABLE:能修饰局部变量

             (7)ElementType.ANNOTATION_TYPE:能修饰注解

             (8)ElementType.PACKAGE:能修饰包

        2、@Retention : 定义了该Annotation被保留的时间长短,使用这个meta-Annotation可以对注解的“生命周期”限制,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)

             (1)RetentionPolicy.SOURCE:表示注解只在源代码中存在,编译成.class之后就没了,@Override就是这样的注解

             (2)RetentionPolicy.CLASS:注解在java文件编译成.class文件后依然存在,但是运行后就没了。其是@Retention的默认值

             (3)RetentionPolicy.RUNTIME:注解在运行起来后依然存在,程序可以通过反射获取这些信息

        3、@Inherited : 表示该注解具有继承性

                  假设,我们定义了某个Annotaion,它的名称是MyAnnotation,并且MyAnnotation被标注为@Inherited。现在,某个类Base使用了MyAnnotation,则Base具有了“具有了注解MyAnnotation”;现在,Sub继承了Base,由于MyAnnotation是@Inherited的(具有继承性),所以,Sub也“具有了注解MyAnnotation”。

//自定义一个可继承的注解
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Inheritable{
}

//应用注解
@Inheritable
class InheritableFather{
    public InheritableFather(){
        System.out.println("InheritableFather "+InheritableFather.class.isAnnotationPresent(Inheritable.class));
    }
}

//继承应用了注解的类
public class InheritedTest extends InheritableFather{

    public InheritedTest(){
        super();//调用父类构造函数
        //查看InheritedTest类是否具有Inheritable注解
        System.out.println("InheritedTest "+InheritedTest.class.isAnnotationPresent(Inheritable.class));
    }

}

          当使用InheritedTest构造函数后可以得到信息

InheritableFather true
InheritedTest true

           所以,子类成功继承了注解@Iheritable

            

        4、@Documented :一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中。

              @Documented 用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化,Documented是一个标记注解,没有成员。

              Documented 注解表明这个注解应该被 javadoc工具记录,默认情况下javadoc是不包括注解的,但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中。 

        5、@Repeatable :(java 1.8新增)当没有@Repeatable修饰的时候,注解在同一个位置,只能出现一次

                                       而进行了@Repeatable注解的注解,可以多次出现,动态地提供数据,如例所示:

    //定义一个可重复的注解,其@Repeatable值采用FileTypes
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Repeatable(FileTypes.class)
    public @interface FileType{
        String value();
    }

    //注解FileTypes,其value()返回一个FileType数组
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface FileTypes{
        FileType[] value();
    }

猜你喜欢

转载自www.cnblogs.com/wqq-blog/p/10565520.html
今日推荐