? 问号放在变量后,永远不报空指针
val list: ArrayList<String>? = null;
if(list?.size?: 0 > 0) { //问号放在变量后,永远不报空指针
}
?: A变量?:B变量:当A变量为空时 返回B
when(list?.size ?: 0) { // A变量?:B变量:当A变量为空时 返回B
0
-> Log.i(TAG, "size is 0")
1
-> Log.i(TAG, "size is 1")
else
-> Log.i(TAG, "size > 1")
}
:: 传参是方法
Log.i(TAG, "-> addFunction: " + chengFunction(1,2,::addFunction))
fun addFunction(a: Int, b: Int): Int {
return a+b;
}
fun chengFunction(a: Int, b: Int, method: (a: Int, b: Int) -> Int): Int {
return a * b * method(a,b);
}