@Test
fun applyAndAlso() {
Car().apply { id = 10; name = "dd" }.let { println(it) }
Car().also { it.id = 10; it.name = "dd" }.let { println(it) }
data class Car(var id: Int? = null, var name: String? = null)
this result is :
Car(id=10, name=dd)
Car(id=10, name=dd)
@Test
fun letAndAlso() {
"car1".apply { reversed()}.let { println(it) }
"car1".also { it.reversed()}.let { println(it) }
"car1".let { it.reversed()}.let { println(it) }
}
this result is :
car1
car1
1rac
my understand : apply and also is same things but pass the param is different .
1 T.apply和T.also是一对,它们的返回值为this,他们的区别在于apply block参数中传递的是this,also block参数中传递的是it
2 T.run和T.let是一对,它们的返回值是block执行的结果,它们的区别在于run block参数传递的是this,let block参数传递的是it
3 run和with是一对,run block参数中既不是this也不是it。with block参数是this