<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### when條件語句 when表達式類似于 switch-case 表達式。when取代了類 C 語言的 switch 操作符,when會對所有的分支進行檢查直到有一個條件滿足。但相比switch而言,when語句要更加的強大,靈活。 * 給定條件 * 滿足什么條件執行什么任務 **示例如下** ``` package B流控語句 fun main(args: Array<String>) { var x = 0 when (x) { //是否是Int類型 is Int -> println("${x}是Int類型") //是否是0或者1 0, 1 -> println("${x}不是0,就是1") //是否在[0,10]區間 in 0..10 -> println("${x}在[0,10]") //是否不在[0,10]區間 !in 0..10 -> println("${x}不在[0,10]區間") //是否是1 1 -> println("${x}是1") //是否是2 2 -> println("${x}是2") //如果都不滿足 else -> println("所有情況都不滿足") } testWhen(1) } fun testWhen(obj: Any) { when (obj) { 1 -> print("第一項") "hello" -> print("這個是字符串hello") is Long -> print("這是一個Long類型數據") is Int -> print("這是一個Int類型數據") !is String -> print("這不是String類型的數據") else -> print("else類似于Java中的default") } } ``` 運行結果 ``` 0是Int類型 第一項 Process finished with exit code 0 ``` * when 將它的參數與所有的分支條件順序比較,直到某個分支滿足條件。 * when 既可以被當做表達式使用也可以被當做語句使用。 * 如果它被當做表達式, 符合條件的分支的值就是整個表達式的值, * 如果當做語句使用, 則忽略個別分支的值。(像 if 一樣,每一個分支可以是一個代碼塊,它的值是塊中最后的表達式的值。) * 如果其他分支都不滿足條件將會求值 else 分支。 **如果 when 作為一個表達式使用,則必須有 else 分支, 除非編譯器能夠檢測出所有的可能情況都已經覆蓋了**[例如,對于 [枚舉(enum)類](http://www.kotlincn.net/docs/reference/enum-classes.html)條目與[密封(sealed)類](http://www.kotlincn.net/docs/reference/sealed-classes.html)子類型]。 * 如果很多分支需要用相同的方式處理,則可以把多個分支條件放在一起,用逗號分隔: ``` when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") } ``` * 我們可以用任意表達式(而不只是常量)作為分支條件 ``` when (x) { parseInt(s) -> print("s encodes x") else -> print("s does not encode x") } ``` * 我們也可以檢測一個值在(in)或者不在(!in)一個區間或者集合中: ``` when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } ``` * 另一種可能性是檢測一個值是(*is*)或者不是(*!is*)一個特定類型的值。 >[info]注意: 由于[智能轉換](http://www.kotlincn.net/docs/reference/typecasts.html#智能轉換),你可以訪問該類型的方法與屬性而無需任何額外的檢測。 ``` fun hasPrefix(x: Any) = when(x) { is String -> x.startsWith("prefix") else -> false } ``` * when也可以用來取代`if-else` if 鏈。 如果不提供參數,所有的分支條件都是簡單的布爾表達式,而當一個分支的條件為真時則執行該分支: ``` when { x.isOdd() -> print("x is odd") x.isEven() -> print("x is even") else -> print("x is funny") } ``` 示例 ``` fun main(args: Array<String>) { var a: Int = 7 var b: Int = 8 when { a > b -> print("a大于b") a < b -> print("a小于b") else -> print("a等于b") } } ``` 運行結果 ``` a小于b ``` * 自 Kotlin 1.3 以來, 可以使用以下語法捕獲變量中的主題,變量的范圍, 在主體時引入, 僅限于主體時。 ``` fun Request.getBody() = when (val response = executeRequest()) { is Success -> response.body is HttpError -> throw HttpException(response.status) } ```
                  <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>

                              哎呀哎呀视频在线观看