博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
猿来绘Java-44-自定义注解和元注解
阅读量:2094 次
发布时间:2019-04-29

本文共 3822 字,大约阅读时间需要 12 分钟。

自定义注解

定义新的 Annotation 类型使用 @interface 关键字,自定义注解是,可以参照Java中现有的注解,例如:@SuppressWarnings

自定义注解自动继承了java.lang.annotation.Annotation接口

Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明。 其方法名和返回值定义了该成员的名字和类型。 我们称为配置参数。 类型只能是八种基本数据类型、 String类型、 Class类型、 enum类型、 Annotation类型、以上所有类型的数组。

可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字

如果只有一个参数成员, 建议使用参数名为value

如果定义的注解含有配置参数, 那么使用时必须指定参数值, 除非它有默认值。 格式是“参数名 = 参数值” , 如果只有一个参数成员, 且名称为value,可以省略“value=”

没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation

注意: 自定义注解必须配上注解的信息处理流程才有意义。

package com.ylaihui.annotations;public @interface MyAnnotations {    String[]  value() default "myanno";}

//MyAnnotationTest.javapackage com.ylaihui.annotations;//@MyAnnotations("myanno")@MyAnnotationsinterface Info{    public void show();}public class MyAnnotationTest {    public static void main(String[] args) {    }}

4种元注解的使用

比如新购买的华为手机,有一本说明书,说明书中的前一页有一段说明,是对说明书说明

 类比元注解,就是对现有注解的注解,对现有的注解的解释说明。

JDK5.0提供了4个标准的meta-annotation类型, 分别是:

Retention

Target

Documented

Inherited

Retention

Retention:指定所修饰的 Annotation 的生命周期:SOURCE\CLASS(默认行为)\RUNTIME 只有声明为RUNTIME生命周期的注解,才能通过反射获取。

SOURCE: Annotations are to be discarded by the compiler. 编译后的class文件不会有注解信息

CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. class文件有注解信息,但是运行时无法得到注解信息

RUNTIME: Annotations are to be recorded in the class file by the compiler and retained by the VM at run time,运行时可以获取注解信息,反射的时候用到

代码示例:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.SOURCE)public @interface SuppressWarnings {

Target

@Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素。 @Target 也包含一个名为 value 的成员变量。

Target:用于指定所修饰的 Annotation 能用于修饰哪些程序元素

用法举例

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.SOURCE)public @interface SuppressWarnings {.........

注解 SuppressWarnings 上面用Target修饰, 表示 SuppressWarnings 可以修饰 类型、属性、方法、参数、构造器、局部变量, 如果 某个注解声明为 @Target({TYPE}), 那么只可以修饰类型,不可以修饰属性方法等

Documented

Documented:表示所修饰的注解在被javadoc解析时,保留下来。

@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被javadoc 工具提取成文档。 默认情况下, javadoc是不包括注解的。

定义为Documented的注解必须设置Retention值为RUNTIME。

用法举例:

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})public @interface Deprecated {}

Inherited

@Inherited: 被它修饰的 Annotation 将具有继承性。如果某个类使用了被@Inherited 修饰的 Annotation, 则其子类将自动具有该注解。

比如:如果把标有@Inherited注解的自定义的注解标注在类级别上,子类则可以继承父类类级别的注解,实际应用中,使用较少

Inherited:被它修饰的 Annotation 将具有继承性。

某个类使用了注解 MyAnnotations,如果注解MyAnnotations有继承性,也就是被 @Inherited修饰,那么子类也继承了父类的注解。

package com.ylaihui.annotation1;import java.lang.annotation.*;import static java.lang.annotation.ElementType.FIELD;import static java.lang.annotation.ElementType.TYPE;@Documented  // 所修饰的注解 MyAnnotation 在被javadoc解析时,保留下来@Retention(RetentionPolicy.RUNTIME)  // MyAnnotation 注解信息 保留到运行时,反射可以获取注解信息@Target({TYPE, FIELD})   // MyAnnotation 注解可以修饰 类型、属性@Inherited  // 被它修饰 MyAnnotation 将具有继承性,父类是 MyAnnotation,其子类也是 MyAnnotationpublic @interface MyAnnotation {    String[] value() default "ylaihui";}
//AnnotationTest.javapackage com.ylaihui.annotation1;import java.lang.annotation.Annotation;@MyAnnotationclass Person{    @MyAnnotation    String name;    int age;    // '@MyAnnotation' not applicable to method    //@MyAnnotation    public void show(){        System.out.println();    }}class Student extends Person{}public class AnnotationTest {    public static void main(String[] args) {        // 子类 Student 继承了父类 Person的注解 MyAnnotation,        // 因为 MyAnnotation 具有继承性是声明为 Inherited 的        Annotation[] annotations = Student.class.getAnnotations();        for (int i = 0; i < annotations.length; i++) {            System.out.println(annotations[i]);        }    }}

转载地址:http://kxuhf.baihongyu.com/

你可能感兴趣的文章
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>
attention 机制入门
查看>>
手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
查看>>
GAN 的 keras 实现
查看>>
AI 在 marketing 上的应用
查看>>
Logistic regression 为什么用 sigmoid ?
查看>>
Logistic Regression 为什么用极大似然函数
查看>>
为什么在优化算法中使用指数加权平均
查看>>
Java集合详解1:一文读懂ArrayList,Vector与Stack使用方法和实现原理
查看>>
Java集合详解2:一文读懂Queue和LinkedList
查看>>
Java集合详解4:一文读懂HashMap和HashTable的区别以及常见面试题
查看>>