得饶人处且饶人——曹雪芹
kotlin
习惯用法见:https://www.kotlincn.net/docs/reference/idioms.html
这里试试延迟属性:
https://www.kotlincn.net/docs/reference/delegated-properties.html#延迟属性-lazy
代码语言:javascript复制val lazyValue: String by lazy {
println("computed!")
"Hello"
}
println(lazyValue)
println(lazyValue)
运行结果:
代码语言:javascript复制computed!
Hello
Hello