Java SE 三个基本注解(JDK内置)+四个元注解

使用注解(Annotation)时要在前面加@符号,注解可以当作一个修饰符来修饰他支持的修饰的元素
@Override - 重写,该注解只能用于方法
@Deprecated - 已弃用,用在程序元素上,如某个类上或者某个方法上
@SuppressWarnings - 抑制编译器警告

@Target - 指定注解可以在哪些地方使用

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation interface
     * can be applied to.
     * @return an array of the kinds of elements an annotation interface
     * can be applied to
     */
    ElementType[] value();
}

@Retention - 指定注解作用范围,三种范围SOURCE,CLASS,RUNTIME

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * 作用在源码层面(.java文件层面),会被编译器丢弃,即不会编译进.class文件中
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 作用在字节码层面(.class文件层面),会被编译器记录,即会编译进.class文件中,但不会被JVM保留,即运行
     * 时被丢弃
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * 作用在JVM层面(.class文件层面),会被编译器记录,即会编译进.class文件中,也会被JVM保留,即在JVM上运行时也能被反射
     * 机制读取到
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

@Documented - 是否在JavaDoc里面体现

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited - 子类会继承父类注解

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDK 中,内置了很多注解,其中有些注解定义了参数,可以通过这些参数来定制注解的行为。下面是使用 JDK 内置注解参数的一些规则: 1. 定义注解参数时需要使用 @interface 关键字,并且参数类型只能是基本数据类型、String、Class、枚举类型、注解类型或这些类型的数组。 2. 参数定义时需要指定参数名和默认值。例如: ```java public @interface MyAnnotation { int value() default 0; String name() default ""; } ``` 上述代码定义了一个注解类型 MyAnnotation,其中包含了两个参数:value 和 name。value 参数类型为 int,且默认值为 0;name 参数类型为 String,且默认值为空字符串。 3. 在使用注解时,可以省略参数名,直接指定参数值。例如: ```java @MyAnnotation(10, "Hello") public void myMethod() { // ... } ``` 上述代码使用了 MyAnnotation 注解,并为 value 参数指定了值 10,为 name 参数指定了值 "Hello"。 4. 如果只需要为某些参数指定值,而其他参数使用默认值,可以使用以下形式: ```java @MyAnnotation(value = 10) public void myMethod() { // ... } ``` 上述代码只为 value 参数指定了值 10,name 参数依然使用默认值。 5. 如果参数类型是枚举类型,可以使用以下形式指定参数值: ```java public enum MyEnum { VALUE1, VALUE2, VALUE3; } @MyAnnotation(MyEnum.VALUE1) public void myMethod() { // ... } ``` 上述代码使用了枚举类型 MyEnum,并为 MyAnnotation 注解的参数指定了枚举常量 MyEnum.VALUE1。 6. 如果参数类型是注解类型,可以使用以下形式指定参数值: ```java public @interface OtherAnnotation { int value(); } @MyAnnotation(@OtherAnnotation(10)) public void myMethod() { // ... } ``` 上述代码定义了一个注解类型 OtherAnnotation,并为 value 参数指定了值 10。然后在使用 MyAnnotation 注解时,将 OtherAnnotation 作为参数值传入。 以上就是使用 JDK 内置注解参数的一些规则。需要注意的是,注解参数只能用于限制编译时期的行为,不能影响运行时期的行为。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值