Kotlin中let,with,apply,run,also的區別

//let apply with
senoirManager.let {
    it.drive()
//        println(this)  this表示 該類的this指針 it表示該對象
    println(it)
}

val applyResult = senoirManager.apply {
    this.drive()

    //apply返回的 是該對象
}
println("applyResult:$applyResult")

val withResult = with(senoirManager) {
    this.drive()
    //with 返回的是最後一行的值得
    100
}
println("withResult:$withResult")

一張圖 說明之間的區別
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章