[英]How to write a member in Kotlin annotation?
我有下一个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 。
它可能是“ KClass”;
@Retention(RetentionPolicy.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.