正如我的评论中已经建议的那样,使用.apply您可以将调用 链接init在一起,+=因为apply返回其调用的目标。
如果您更喜欢使用init(),您可以使用 获得相同的结果
val tr = TR()
children += tr
tr.init()
链式变体的关键方面是应用function of the Kotlin's standard library is defined as an extension function of a generic typeT, accepting a *lambda with receiver作为其唯一参数,如您在此处看到的:
inline fun <T> T.apply(block: T.() -> Unit): T
为了解释它的含义,你可以自己实现这个函数:
fun <T> T.myApply(block: T.() -> Unit) : T {
this.block()
return this
}
以下示例模仿您的代码,使用假MyClass类型代替原始类型TR:
fun <T> T.myApply(block: T.() -> Unit) : T {
this.block()
return this
}
class MyClass(val text: String) {
fun foo() : Unit {
println("foo $text")
}
}
fun initializer(mc: MyClass) {
println("initializer ${mc.text}")
mc.foo()
}
fun run(init: MyClass.() -> Unit) {
val result = MyClass("first").myApply(init)
val x = MyClass("second")
x.init()
}
fun main(args: Array<String>) {
run(::initializer)
}
您可以使用此示例来遵循从run到的流程MyClass.foo,通过接受init作为带有接收器参数的 lambda的函数:我希望这可以帮助您澄清您对原始和替代实现的关键特征的理解tr。