我使用接口(interface)委托(delegate)描述here .有没有办法使用它但覆盖并显式调用父类(super class)(委托(delegate))?
这些方面的东西(不编译):
class Rect(val width: Int, val height: Int, position: Position) : Positionable by DefaultPositionable(position) {
override fun getPosition() {
val parent = super.getPosition()
return parent.copy(x = parent.x + 1)
}
}
最佳答案
您无法访问 delegate
在这种情况下。作为解决方法,您可以使用下一种方法
class Rect(
val width: Int,
val height: Int,
val pos: Position,
private val positinable: Positionable = DefaultPositionable(pos)
) : Positionable by positinable {
override fun getPosition(): Position {
val parent = positinable.getPosition()
return parent.copy(x = parent.x + 1)
}
}
关于kotlin - 在 Kotlin 中使用接口(interface)委托(delegate)时可以显式调用父类(super class)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53280224/