初始化Kotlin中主构造函数中具有setter的属性

初始化Kotlin中主构造函数中具有setter的属性,kotlin,constructor,properties,primary-constructor,Kotlin,Constructor,Properties,Primary Constructor,我有以下代码: class Camera : AsyncActiveInputDevice<Image> { constructor(inputListener: ((Image) -> Unit)? = null) { this.inputListener = inputListener } override var inputListener: ((Image) -> Unit)? set(value) {

我有以下代码:

class Camera : AsyncActiveInputDevice<Image> {
    constructor(inputListener: ((Image) -> Unit)? = null) {
        this.inputListener = inputListener
    }

    override var inputListener: ((Image) -> Unit)?
        set(value) {
            field = value
            TODO("call C/Python implementation")
        }
}
类摄像机:AsyncActiveInputDevice{
构造函数(inputListener:((图像)->单位)?=null){
this.inputListener=inputListener
}
覆盖变量inputListener:((图像)->单位)?
设置(值){
字段=值
TODO(“调用C/Python实现”)
}
}
IntelliJ IDEA建议将构造函数转换为主构造函数


那么如何转换呢?如何在主构造函数中使用setter初始化属性?我尝试了
init
块,但是它显示了一个错误:“在声明之前无法初始化变量”。

这样的主构造函数将放在类的头中,如下所示:

class Camera(inputListener: ((Image) -> Unit)? = null) : AsyncActiveInputDevice<Image> {

    override var inputListener: ((Image) -> Unit)? = inputListener
        set(value) {
            field = value
            TODO("call C/Python implementation")
        }

}
类摄像机(inputListener:((图像)->单位)?=null:AsyncActiveInputDevice{
重写变量inputListener:((图像)->Unit)?=inputListener
设置(值){
字段=值
TODO(“调用C/Python实现”)
}
}

您可以通过调用警告上的意图操作(Windows上的Alt+Enter、,⌥↩ 在macOS上),并选择Convert to primary constructor。

这样的主构造函数将出现在类的标题中,如下所示:

class Camera(inputListener: ((Image) -> Unit)? = null) : AsyncActiveInputDevice<Image> {

    override var inputListener: ((Image) -> Unit)? = inputListener
        set(value) {
            field = value
            TODO("call C/Python implementation")
        }

}
类摄像机(inputListener:((图像)->单位)?=null:AsyncActiveInputDevice{
重写变量inputListener:((图像)->Unit)?=inputListener
设置(值){
字段=值
TODO(“调用C/Python实现”)
}
}

您可以通过调用警告上的意图操作(Windows上的Alt+Enter、,⌥↩ 在macOS上),并选择Convert to primary constructor。

初始化块必须位于变量声明之后。这就是错误消息告诉您的:

class Camera(inputListener: ((Image) -> Unit)? = null): AsyncActiveInputDevice<Image> {

    override var inputListener: ((Image) -> Unit)? = inputListener
        set(value) {
            field = value
            TODO("call C/Python implementation")
        }


    init {
        this.inputListener = inputListener
    }
}
类摄像机(inputListener:((图像)->单位)?=null:AsyncActiveInputDevice{
重写变量inputListener:((图像)->Unit)?=inputListener
设置(值){
字段=值
TODO(“调用C/Python实现”)
}
初始化{
this.inputListener=inputListener
}
}

init块必须位于变量声明之后。这就是错误消息告诉您的:

class Camera(inputListener: ((Image) -> Unit)? = null): AsyncActiveInputDevice<Image> {

    override var inputListener: ((Image) -> Unit)? = inputListener
        set(value) {
            field = value
            TODO("call C/Python implementation")
        }


    init {
        this.inputListener = inputListener
    }
}
类摄像机(inputListener:((图像)->单位)?=null:AsyncActiveInputDevice{
重写变量inputListener:((图像)->Unit)?=inputListener
设置(值){
字段=值
TODO(“调用C/Python实现”)
}
初始化{
this.inputListener=inputListener
}
}

在此基础上展开:主构造函数中不支持自定义getter和setter,因为在此基础上展开:主构造函数中不支持自定义getter和setter,因为