无需 getter 即可直接从 Java 访问 kotlin public 字段

Access kotlin public field from Java directly without getter

下面是来自 Android 的模式示例(只是一个示例,对 android 细节不感兴趣):

/*Im a kotlin file*/
class ListItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val text: = itemView.my_view
}

那么模式就是您这样访问文本字段:

/*Im a Java file*/
holder.text.setText("Metasyntactic variable");

不幸的是,有一个具有集合结构的大文件执行上述操作,然后有:

/*Im a Java file, but this particular holder is a kotlin file*/
holder.getText().setText("Metasyntactic variable");

有办法解决吗?也许有一些 @Jvm 注释

@JvmField

If you need to expose a Kotlin property as a field in Java, you need to annotate it with the @JvmField annotation. The field will have the same visibility as the underlying property. You can annotate a property with @JvmField if it has a backing field, is not private, does not have open, override or const modifiers, and is not a delegated property.