kotlin,null let expression
class Num {
var data: Int? = -1
}
fun main(args: Array<String>) {
val n = Num()
n.data = null
var b = true
b = n.data?.let { it > 0 } ?: false
println(b)
n.data = 1
b = n.data?.let { it > 0 } ?: false
println(b)
}
false
true
Kotlin common expressions let,?:, as?,?.,!!_zhangphil's blog-CSDN blog it.todo() //Use it in the function instead of the object object to access properties and methods. it.todo() //The let function will be executed only if the object is not null. When a is not null, execute the statement in the braces (it is definitely not null) if a == null, then it is null. = null, then ab() is null if a is not b. If a is b, then a as b. When a is null, nothing is executed. If a==null, throw a null pointer. =null, execute a. When a==null, execute b. https://blog.csdn.net/zhangphil/article/details/129264159
Kotlin's let, with, run, apply, also, similarities and differences_zhangphil's blog-CSDN blog it.todo() //The let function will only be executed if the object is not null. When a is not null, execute the statement in the braces (it must not be null) if a == null, then it is null. = null, then ab() is null if a is not b. If a is b, then a as b. When a is null, nothing is executed. If a==null, throw a null pointer. =null, execute a. When a==null, execute b. Kotlin common expression let, ?zhang 19 worked 996, earning 59760. Zhang 20 works 996 and earns 59760. https://blog.csdn.net/zhangphil/article/details/129327285