我有下一个 Java 注释:
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewModelKey {
Class<? extends ViewModel> value();
}
为了在 Kotlin 注释中转换它,我将其重写如下:
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey {
fun value(): Class<out ViewModel> {}
}
但是有一个错误:Members are not allowed in annotation class
。
如果不允许使用成员,我该如何在 Kotlin 中转换 Java 注释?
最佳答案
在 Kotlin 中,您必须在注释中定义属性而不是函数:
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
参见 https://kotlinlang.org/docs/reference/annotations.html了解详情。
关于java - 如何在 Kotlin 注解中写一个成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592797/