<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ### **“let”函數** let 函數讓處理可空表達式變得更容易。和安全調用運算符一起使用,可以對表達式求值,檢查求值結果是否為null,并把結果保存為一個變量。所有這些動作都在同一個簡潔的表達式中。 可空參數最常見的一種用法應該就是被傳遞給一個接收非空參數的函數。比如說下面這個sendEmailTo 函數,它接收一個String 類型的參數井向這個地址發送一封郵件。 ~~~ fun main(args: Array<String>) { val email:String?="123456@163.com" sendEmailTo(email)//這里報錯如下,就是說,這個函數需要一個非空參數,而此時傳入了可空參數 // Error:(5, 17) Kotlin: Type mismatch: inferred type is String? but String was expected } fun sendEmailTo(email:String){/*.....*/} ~~~ 必須要顯示地檢查這個值是否為空,比如修改為`if (email != null) sendEmailTo(email) ` 這里還可以這樣處理(使用let函數),使用let 函數,并通過安全調用來調用它。let函數做的所有事情就是把一個調用它的對象變成lambda 表達式的參數。如果結合安全調用語法,它能有效地把調用let 函數的可空對象,轉變成非空類型 ![](https://box.kancloud.cn/c9d8c894e08689480c369bd615ae7cf0_562x157.png) 安全調用“ let”只在表達式不為null 時執行lambda 上面的代碼就可這樣寫 ~~~ email?.let { email -> sendEmailTo(email) }//let 函數只在email 的值非空時才被調用 ~~~ 使用自動生成的名字it 這種簡明語法之后,上面的代碼就更短了: ~~~ email?.let { sendEmailTo(it) } ~~~ 完整代碼如下 ~~~ fun main(args: Array<String>) { var email:String?="wsc123@163.com" // //直接下面這種方式,會報錯 // sendEmailTo(email)//這里報錯如下,就是說,這個函數需要一個非空參數,而此時傳入了可空參數 // //第一種方法 // if (email!=null){ // // sendEmailTo(email) // } //第二種方法使用let函數 email?.let { email -> sendEmailTo(email) } //使用自動生成的名字it 這種簡明語法,代碼更簡短 email?.let { sendEmailTo(it) } email = null email?.let { sendEmailTo(it) } } fun sendEmailTo(email:String){ println("Send email to $email") } ~~~ 運行結果 ``` Send email to wsc123@163.com Send email to wsc123@163.com Process finished with exit code 0 ``` 注意,如果有一些很長的表達式結果不為null,而你又要使用這些結果時,let表示法特別方便。這種情況下你不必創建一個單獨的變量。對比一下顯式的if檢查 **if顯示檢查** ~~~ val person:Person?=getTheBestPersonTheWorld() if(person !=null) sendEmailTo(person.email) ~~~ **let函數代碼** ~~~ getTheBestPersonTheWorld()?.let{sendEmailTo(it.email)} ~~~ 當你需要檢查多個值是否為null 時,可以用嵌套的let 調用來處理。但在大多數情況下,這種代碼相當啰嗦又難以理解。用普通的if 表達式來一次性檢查所有值通常更簡單。 ### **可空類性的擴展** 為可空類型定義擴展函數是一種更強大的處理null 值的方式。可以允許接收者為null 的(擴展函數)調用,并在該函數中處理null ,而不是在確保變量不為null之后再調用它的方法。只有擴展函數才能做到這一點,因為普通成員方法的調用是通過對象實例來分發的,因此實例為null 時(成員方法)永遠不能被執行。 Kotlin 標準庫中定義的String 的兩個擴展函數isEmpty和isBlank就是這樣的例子。第一個函數判斷字符串是否是一個空的字符串"" 。第二個函數則判斷它是否是空的或者它只包含空白字符。 示例如下 ~~~ fun main(args: Array<String>) { verifyUserInput(" ") verifyUserInput(null)//這個接受者調用isNullOrBlank并不會導致任何異常 } fun verifyUserInput(input:String?){ if (input.isNullOrBlank()){ println("Please fill in the required fields") } } ~~~ 運行結果 ``` Please fill in the required fields Please fill in the required fields Process finished with exit code 0 ``` 不需要安全訪問,可以直接調用為可空接收者聲明的擴展函數(如下圖所示)。這個函數會處理可能的null值。 ![](https://box.kancloud.cn/95638baa8de54663180a11d59af12ceb_401x167.png) 函數isNullOrBlank 顯式地檢查了null ,這種情況下返回true ,然后調用isBlank ,它只能在非空String 上調用: ~~~ //可空字符串的擴展,第二個this使用了智能轉換 fun String?.isNullOrBlank():Boolean = this ==null || this.isBlank() ~~~ 當你為一個可空類型(以?結尾)定義擴展函數時,這意味著你可以對可空的值調用這個函數;并且**函數體中的this 可能為null **,所以你**必須顯式地檢查**。在Java 中, this 永遠是非空的,因為它引用的是當前你所在這個類的實例。而在Kotlin 中,這并不永遠成立:**在可空類型的擴展函數中, this 可以為null** 。 >[info]注意:當你定義自己的擴展函數時,需要考慮該擴展是否需要為可空類型定義。默認情況下,應該把它定義成非空類型的擴展函數。如果發現大部分情況下需要在可空類型上使用這個函數,你可以稍后再安全地修改它(不會破壞其他代碼) 。 ### **類型參數的可空性** Kotlin 中所有泛型類和泛型函數的類型參數默認都是可空的。任何類型,包括可空類型在內,都可以替換類型參數。這種情況下,使用類型參數作為類型的聲明都允許為null,盡管類型參數T井沒有用問號結尾。 ~~~ fun main(args: Array<String>) { printHashCode(null)//“T”被推導成“Any?” } fun <T> printHashCode(t:T){ println(t?.hashCode())//因為t可能是null,所以必須使用安全調用 } ~~~ 在printHashCode調用中,類型參數T 推導出的類型是可空類型Any?。因此,盡管沒有用問號結尾,實參t 依然允許持有null 。 要使類型參數非空,必須要為它指定一個非空的上界,那樣泛型會拒絕可空值作為實參。 ~~~ fun main(args: Array<String>) { printHashCode(42) //printHashCode(null)//這段無法編譯,因為不能傳遞null,需要的是非空值 //Error:(5, 5) Kotlin: Type parameter bound for T in fun <T : Any> printHashCode(t: T): Unit is not satisfied: inferred type Nothing? is not a subtype of Any } fun <T:Any> printHashCode(t:T){//現在的“T”就不是可空的 println(t?.hashCode()) } ~~~ 運行結果 ``` 42 Process finished with exit code 0 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看