interfaceBase{ fun printMessage() fun printMessageLine() } class BaseImpl(val x: Int) : Base { override fun printMessage(){ print(x) } override fun printMessageLine(){ println(x) } }
class Derived(b: Base) : Base by b { override fun printMessage(){ print("abc") } }
fun main(){ val b = BaseImpl(10) Derived(b).printMessage() Derived(b).printMessageLine() }
interfaceBase{ val message: String fun print() } class BaseImpl(val x: Int) : Base { override val message = "BaseImpl: x = $x" override fun print(){ println(message) } }
class Derived(b: Base) : Base by b { // 在 b 的 `print` 实现中不会访问到这个属性 override val message = "Message of Derived" }
fun main(){ val b = BaseImpl(10) val derived = Derived(b) derived.print() println(derived.message) }