欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Kotlin 基础教程之注解与java中的注解比较

程序员文章站 2023-12-15 12:17:22
kotlin 的注解完全兼容 java 的注解。 声明注解 @target(annotationtarget.class, annotationtarget...

kotlin 的注解完全兼容 java 的注解。

声明注解

@target(annotationtarget.class, annotationtarget.function,
annotationtarget.value_parameter, annotationtarget.expression)
@retention(annotationretention.source)
@mustbedocumented
annotation class fancy

可以通过向注解类添加元注解(meta-annotation)的方法来指定其他属性:

@target 指定这个注解可被用于哪些元素(类, 函数, 属性, 表达式, 等等.);

@retention 指定这个注解的信息是否被保存到编译后的 class 文件中, 以及在运行时是否可以通过反
射访问到它;

@repeatable 允许在单个元素上多次使用同一个注解;

@mustbedocumented 表示这个注解是公开 api 的一部分, 在自动产生的 api 文档的类或者函数签名
中, 应该包含这个注解的信息。

使用

@fancy class foo {
  @fancy fun baz(@fancy foo: int): int {
    return (@fancy 1)
  }
}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: