【发布时间】:2021-06-20 05:47:33
【问题描述】:
从官方文档中复制/粘贴: https://kotlinlang.org/docs/delegated-properties.html#delegating-to-another-property
var topLevelInt: Int = 0
class ClassWithDelegate(val anotherClassInt: Int)
class MyClass(var memberInt: Int, val anotherClassInstance: ClassWithDelegate) {
var delegatedToMember: Int by this::memberInt
var delegatedToTopLevel: Int by ::topLevelInt
val delegatedToAnotherClass: Int by anotherClassInstance::anotherClassInt
}
var MyClass.extDelegated: Int by ::topLevelInt
我想我需要导入一些包,就像这个答案一样,但是对于Intellij,而不是Jetpack Compose:https://stackoverflow.com/a/63877349/10777336
【问题讨论】:
-
我刚刚在Play Kotlin 中尝试过,它成功了。你的 Kotlin 版本是什么?
-
哦,原来是 1.3,我把它改成 1.5 就可以了。谢谢!
标签: kotlin