Yes, I can provide a working example. I even could provide a working sample that uses delegation, but it is quite messy. So, instead I refer back to XY Problem (mentioned by arocnies above) and what darksnake said in the other thread:
Therefore I provide a working example using interfaces (JumpableImpl
renamed to StaminaJumpable
):
/** A jumper that consumes stamina corresponding to the height of its jumps. */
interface StaminaJumpable {
fun jump() {
spendStamina(height())
}
fun spendStamina(usedAmount: Int)
fun height(): Int
}
class Frog(
swimable: Swimable = SwimableImpl()
) : Swimable by swimable, StaminaJumpable {
var age = 42
var stamina = 128
private set
override fun spendStamina(usedAmount: Int) {
println("Spent stamina: $usedAmount")
this.stamina -= usedAmount
}
override fun height() = age * 2
}