如何在Kotlin中创建一个具有私有getter(或根本没有)但具有公共setter的属性?

var status
private get

出现错误时不工作:Getter visibility must be the same as property visibility

In my case, the reason is for Java interop: I want my Java code to be able to call setStatus but not getStatus.

推荐答案

It's impossible at the moment in Kotlin to have a property with a setter that is more visible than the property. There's a language design issue in the issue tracker on this, feel free to watch/vote for it or share your use cases: https://youtrack.jetbrains.com/issue/KT-3110

Kotlin相关问答推荐

KMP:未能添加cafe.adriel.voyager依赖项

无法访问类kotlin.coroutines.CoroutineContext';.判断模块类路径中是否存在丢失或冲突的依赖项

编译后的JavaFX应用程序立即以静默方式崩溃

在 kotlin 原始字符串中转义三重引号

Kotlin 如何使用其 get 函数在内部检索映射值

如何将glide显示的图像下载到设备存储中.Kotlin

如何将jooq multiset的结果映射到Hashmap(Java Map)?

如何从 kotlin 中的数据类访问 val?

为什么 android studio 不为所有安全参数生成代码?

将 Kotlin 类属性设置器作为函数引用

找不到 androidsdk.modules

`DataBindingUtil` 中的 `bind`、`inflate` 和 `setContentView` 有什么区别

Foo::class.java 和 Foo::javaClass 有什么区别?

将 Android Studio 升级到 3.1.0 后的 Android Support 插件错误

Kotlin中的下划线名称是为什么保留的?

Kotlin中OnclickListener方法之间的差异

Kotlin中保留的关键字是什么?

可以在函数参数中使用解构吗?

var str:String是可变的还是不可变的?

具有多个 parameter的 Kotlin 枚举