Kotlin覆盖Java setter

时间:2018-01-30 13:03:26

标签: java oop inheritance kotlin

我正在使用此方法实现java接口(Command):

void setInputStream(InputStream in);

我想用Kotlin setter覆盖它:

class ProxyCommand : Command {
    lateinit var _inputStream: ChannelPipedInputStream

    var inputStream: InputStream
        get() = this._inputStream
        set(value) { // This should override it.
            this._inputStream = (value as ChannelPipedInputStream)
        }
}

但我在set(value)收到此错误:

Accidental override: The following declarations have the same JVM signature (setInputStream(Ljava/io/InputStream;)V): 
public final fun <set-inputStream>(value: InputStream): Unit defined in ...
public abstract fun setInputStream(`in`: InputStream!): Unit defined in ...

它说它是偶然的覆盖,但它不是偶然的......

我想要的是什么?或者我必须覆盖setInputStream方法。我更喜欢kotlin setter ..

1 个答案:

答案 0 :(得分:-1)

  

或者我必须覆盖setInputStream方法。我更喜欢kotlin setter ..

tldr:是的

属性设置器无法覆盖超类型Command的功能。作为一种解决方法,请使您的属性private并实现所需的接口方法,如下所示:

class ProxyCommand : Command {
    override fun setInputStream(`in`: InputStream) {
        TODO("not implemented")
    }

    private lateinit var inputStream: ChannelPipedInputStream
}