当前位置: 移动技术网 > IT编程>开发语言>Java > JDK 5 提供的注解:Target、Inherited和Documented的区别

JDK 5 提供的注解:Target、Inherited和Documented的区别

2019年07月22日  | 移动技术网IT编程  | 我要评论

白银暴跌,监利人吧,小客车指标查询

jdk 5提供的注解,除了retention以外,还有另外三个,即target 、inherited 和 documented。

target 目标即target注解用在哪里, 定义了注解使用的时机,即注解所适用的程序元素的种类。如果注解类型声明中不存在 target 元注解,则声明的类型可以用在任一程序元素上。如果存在这样的元注解,则编译器强制实施指定的使用限制。

target 定义如下:

@documented
@retention(retentionpolicy.runtime)
@target(elementtype.annotation_type)
public @interface target {
  elementtype[] value();
}

可以看到,target 只有一个value属性,类型为枚举类型elementtype。elementtype 声明如下:

public enum elementtype {
  /** 注解可以用在类、接口(包括注解类型)或枚举声明 */
  type,
  /** 字段声明(包括枚举常量) */
  field,
  /** 方法声明 */
  method,
  /** 参数声明 */
  parameter,
  /** 构造方法声明 */
  constructor,
  /** 局部变量声明 */
  local_variable,
  /** 注解类型声明 */
  annotation_type,
  /** 包声明 */
  package
}

documented注解表明制作javadoc时,是否将注解信息加入文档。如果注解在声明时使用了@documented,则在制作javadoc时注解信息会加入javadoc。注解声明如下:

@documented
@retention(value=runtime)
@target(value=annotation_type)//说明该注解只能在声明注解时使用,即元注解
public @interface documented {}

inherited 注解同样是元注解,声明如下:

@documented
@retention(value=runtime)
@target(value=annotation_type)
public @interface inherited {}

inherited 注解表明注解是否会被子类继承,缺省情况是不继承的。当注解在声明时,使用了@inherited注解,则该注解会被使用了该注解的类的子类所继承。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网