当前位置: 移动技术网 > IT编程>开发语言>Java > Kotlin 基础教程之注解与java中的注解比较

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

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

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)
  }
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网