Tag: kotlin委托

调用(基本)委托function时,从内部使用类委托覆盖

当覆盖类委托实现的接口方法时,是否可以从一个覆盖函数中调用通常委派给的类? 与使用inheritance时的super类似。 从文档 : interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main(args: Array) { val b = BaseImpl(10) Derived(b).print() // prints 10 } 请注意,覆盖工作正如您所期望的那样:编译器将使用您的覆盖实现,而不是代理对象中的那些实现。 override fun print() { … } 如何从这个重写的函数中调用BaseImpl print()函数? 用例是我想添加额外的逻辑到这个函数,而重用现有的实现。

如何在Kotlin中实现一个需要另一个属性的懒惰属性?

我需要一个矩形,需要在通话中初始化。 这是我的代码; class EpheButton private constructor( private val text: String, private val x: Float, private val y: Float, private val projectionMatrix: Matrix4) : Disposable { private val spriteBatch: SpriteBatch = SpriteBatch() private val bitmapFont: BitmapFont = BitmapFont() private val shapeRenderer: ShapeRenderer = ShapeRenderer() private val textWidth: Float private val textHeight: Float private val rectangle :Rectangle […]