本地委派属性

局部变量可以使用委派属性。它的一个重要应用场景是懒加载。

val answer by lazy {
    println("Calculating the answer...")
    42
}
if (needAnswer()) { // 随机值
    println("The answer is $answer.") 
} else {
    println("Sometimes no answer is the answer...")
}

results matching ""

    No results matching ""