Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
输出:
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
@Retention: 定义注解的保留策略
-
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
1.2@Target:定义注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解
自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
1.2@Target:定义注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解
自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@Inherited
public @interface Zcy {}上面是一个最简单的注解,复杂一点的注解可以包含自己的变量、枚举、数组,可以设置它们有默认值。public @interface Zcy {
String key() default "abc";
MyEnum value() default MyEnum.Sunday;
int[] size() default {2,4,8};
double[] weight() default 1.0;
float height();
enum MyEnum{Monday,Sunday}
}使用上面的注解:public class Test {
@Zcy(key="china",value=MyEnum.Monday,weight={10.2,25.3,45.0},height=45f)
public void excute(){
System.out.println("method");
}
}由于height没有默认值,所以在使用注解Zcy时必须给出height的值。通过反射获得注解
所有注解是不可变且可序列化的。
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Test t=new Test();
Class<Test> c=Test.class;
//反射,根据名称来获取公有方法
Method method=c.getMethod("excute", new Class[]{});
//判断方法是否有Zcy注解
if(method.isAnnotationPresent(Zcy.class)){
//获取方法的Zcy注解实例
Zcy zcy=method.getAnnotation(Zcy.class);
//执行方法
method.invoke(t, new Object[]{});
//获取注解的成员变量
double[] weight=zcy.weight();
for(double w:weight)
System.out.printf("%f\t", w);
System.out.println();
}
//获取方法上的所有注解(一个方法上可能有多个注解)
Annotation[] annotations=method.getAnnotations();
for(Annotation annotation:annotations)
System.out.println(annotation);
}输出:
excute method
10.200000 25.30000045.000000
@enumeration.Zcy(weight=[10.2, 25.3, 45.0], value=Monday, key=china, size=[2, 4, 8], height=45.0)
10.200000 25.30000045.000000
@enumeration.Zcy(weight=[10.2, 25.3, 45.0], value=Monday, key=china, size=[2, 4, 8], height=45.0)
本文介绍了Java注解的基本概念,包括其用途、元注解的作用及如何自定义注解。同时,还提供了通过反射获取注解的示例。
12万+

被折叠的 条评论
为什么被折叠?



