从文档中我们知道:公共属性的getter方法不能是私有的(这似乎很合理),因此:
@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'
我想知道是否可能拥有带有公共setter的私有属性。