Java注解

注解相当于一个标记,在程序上加上注解就等于在程序上加上了标记,该标记不会影响代码的实际逻辑,仅仅起到了辅助性的作用,JAVAC编译器、开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事

元注解

元注解是注解的注解,用于修饰注解,包括@Retention、@Target、@Document、@Inherited

@Retention注解的保留策略

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();//枚举类型RetentionPolicy有三个可选值,单选
}
  • @Retention(RetentionPolicy.SOURCE)—//注解仅存在于源码中,在class字节码文件中不包含
  • @Retention(RetentionPolicy.CLASS)—//默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
  • @Retention(RetentionPolicy.RUNTIME)—//注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Retention注解的作用目标

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();//枚举类型ElementType的数组有8个可选值,多选
}
  • @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)—//包

@Document说明该注解将被包含在javadoc中

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
    //Documented中没有定义属性
}

@Inherited说明子类可以继承父类中的该注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
    //Inherited中没有属性
}

自定义注解

自定义注解要使用@interface注解,一个注解也是一个类

//定义注解@MyAnnotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value();// 如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。
    String hello() default "hello";
    int[] array() default { 1, 2, 3, 4 };
}

保留策略为RetentionPolicy.RUNTIME的注解,在程序中能使用反射的方式从JVM中访问该注解的信息

public class MyAnnotationTest {
    @MyAnnotation("fjh")
    public void todo() {
        System.out.println("todo method");
    }

    public static void main(String[] args) throws Exception {
        Class<MyAnnotationTest> c = MyAnnotationTest.class;
        Method m = c.getMethod("todo", new Class[] {});
        if (m.isAnnotationPresent(MyAnnotation.class)) {
            m.invoke(new MyAnnotationTest(), null);//todo method
            MyAnnotation myAnnotation = m.getAnnotation(MyAnnotation.class);
            System.out.println(myAnnotation.value());//fjh
            System.out.println(myAnnotation.hello());//hello
            System.out.println(myAnnotation.array().length);//4
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值