如何获得Kotlin的委托实例?

我想获得一个委托类的实例。

具体来说,在下面的例子中,我想获得传递的Baseb一个实例,但尝试使用b时出现错误。

 interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } open class Derived(b: Base) : Base by b { override fun print() { printSomethingBefore() b.print() // b isn't recognized :( printSomethingAfter() } } 

*举例来源: https : //kotlinlang.org/docs/reference/delegation.html

val前缀声明b做了这个诀窍:

…派生( val b:Base):Base by b …