属性 Kotlin 中的可访问性

Property accessibility in Kotlin

这是我们从文档中了解到的信息:getter of public 属性 不能是私有的(似乎合乎逻辑),所以:

@Inject
var repository: MyExampleRepository? = null
    private get

不会编译。 好的,也许我们可以制作 属性 private 并定义 setter public?

@Inject
private var repository: MyExampleRepository? = null
    public set

这将编译并实际注入值,但我仍然不能在代码中使用它,所以:

service.repository = null

给出编译错误:

Kotlin: Cannot access 'repository': it is 'private' in 'MyService'

我想知道是否可以将 属性 与 public setter.

进行私有化

这是一个已知问题:KT-10385

Kotlin doesn't generate setter method for the following code:

private val someProperty: Integer
public set

The intention is to generate a set only property. Use case including spring dependency injection.