5.常见高阶函数4

let,apply
apply操作符相当于将作用域转移到对象内部

data class Person(val name: String, val age: Int){
    fun work(){
        println("$name is working!!!")
    }
}
fun findPerson(): Person?{
    return null
}
fun main() {
    val person= findPerson()
    person?.let {
        println(it.name)
        println(it.age)
        it.work()
    }
    person?.apply {
        println(name)
        println(age)
        work()
    }
}

你可能感兴趣的:(5.常见高阶函数4)