「Java」你知道我们常使用的Java注解,是怎么实现的吗

注解相当于是标记,它与注释的区别在于注解的内容可以可以使用,下面,让我们认识下Java的注解。

要使用注解,我们可以分成三个部分,分别是:注解本身、注解使用类、注解处理类,如下图:

「Java」你知道我们常使用的Java注解,是怎么实现的吗

注解的结构

下面,我们看看代码怎么操作吧

第一步:新建注解

新建两个注解,分别是PlayerName与PlayerInfor

PlayerName:

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

@Documented

public @interface PlayerName {

String value() default "";

}

PlayerInfor:

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

@Documented

public @interface PlayerInfor {

public enum Color{Blue,Red,White};

Color color() default Color.Blue;

int number() default 7;

int age();

String club() default "曼彻斯特联队";

}

第二步:定义实体类,并且使用刚刚新建的注解

新建一个FootballPlayer类

FootballPlayer:

public class FootballPlayer {

//将PlayerName注解绑定在实体类的name属性中

@PlayerName("鲁尼")

public String name;

//将PlayerInfor注解绑定在实体类的playInfo属性中

@PlayerInfor(color=Color.Red,number=10,age=30)

public String playInfo;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPlayInfo() {

return playInfo;

}

public void setPlayInfo(String playInfo) {

this.playInfo = playInfo;

}

}

第三步:定义注解处理类,处理实体类的注解

新建AnnoUtil工具类

AnnoUtil:

import java.lang.reflect.Field;

public class AnnoUtil {

public static void getPlayerInfo(Class> clazz){

/*获取实体类的所有属性*/

Field[] fields = clazz.getFields();

//循环抽取实体类属性中的注解

for(Field field:fields){

//匹配注解

if(field.isAnnotationPresent(PlayerName.class)){

//获取注解

PlayerName name = field.getAnnotation(PlayerName.class);

//使用注解

System.out.println("运动员名字:"+name.value());

}else if(field.isAnnotationPresent(PlayerInfor.class)){

//获取注解

PlayerInfor infor = field.getAnnotation(PlayerInfor.class);

//使用注解

System.out.println("所属球队:"+infor.club()+",代表颜色:"

+infor.color()+",号码:"+infor.number()+",年龄:"+infor.age());

}

}

}

}

第四步:调用处理类的处理方法

public static void main(String[] args) {

AnnoUtil.getPlayerInfo(FootballPlayer.class);

}

输出结果如下:

运动员名字:鲁尼

所属球队:曼彻斯特联队,代表颜色:Red,号码:10,年龄:30

从以上程序可以理解为:实体类与注解的关系就像文章与笔记,而注解处理类就像是人;人读了带有笔记的文章,加以分析处理,输出想要表达的结果。

附:

Java中提供了四种元注解,专门负责注解其他的注解,分别如下

@Retention元注解,表示需要在什么级别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:

RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉

RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)

RetentionPolicy.RUNTIME:内存中的字节码,VM将在运行时也保留注解,因此可以通过反射机制读取注解的信息

@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括

ElementType.CONSTRUCTOR: 构造器声明

ElementType.FIELD: 成员变量、对象、属性(包括enum实例)

ElementType.LOCAL_VARIABLE: 局部变量声明

ElementType.METHOD: 方法声明

ElementType.PACKAGE: 包声明

ElementType.PARAMETER: 参数声明

ElementType.TYPE: 类、接口(包括注解类型)或enum声明

@Documented将注解包含在JavaDoc中

@Inheried允许子类继承父类中的注解


分享到:


相關文章: