美文网首页
Kotlin符号记录: ?、?:、::

Kotlin符号记录: ?、?:、::

作者: 程序狮 | 来源:发表于2022-04-01 16:45 被阅读0次

? 问号放在变量后,永远不报空指针

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);
    }

相关文章

网友评论

      本文标题:Kotlin符号记录: ?、?:、::

      本文链接:https://www.haomeiwen.com/subject/hyvajrtx.html