2016-01-02 54 views
3

以下是我们从文档知道:public财产的getter不能是私有的(似乎很合乎逻辑的),所以:物业无障碍在科特林

@Inject 
var repository: MyExampleRepository? = null 
    private get 

将无法​​编译。 好吧,也许我们可以让财产private和定义设置器public

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

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

service.repository = null

给出编译错误:

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

我想知道是否有可能拥有公共二传手的私人财产。

回答

2

这是一个众所周知的问题: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.