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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## [kotlin.Nothing](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin--nothing.html) 在Kotlin類型層次結構的最底層就是類型 Nothing ![](https://box.kancloud.cn/e535e1e2b81c3013f942ee8490eca1e0_373x315.png) Kotlin中沒有類似Java和C中的函數沒有返回值的標記 void ,但是**擁有一個對應 Nothing** 。在Java中,返回 void 的方法,其返回值 void 是無法被訪問到的。 在Java中, void 不能是變量的類型。也不能被當做值打印輸出。但是,在Java中有個包裝類 Void 是 void 的自動裝箱類型。如果你想讓一個方法返回類型 永遠是 null 的話, 可以把返回類型置為這個大寫的V的 Void 類型。 >[info]【注意】:Unit與Nothing之間的區別: Unit類型表達式計算結果的返回類型是Unit。Nothing類型的表達式計算結果是永遠不會返回的(跟Java 中的 void 相同)。 * 正如它的名字Nothing所暗示的, **Nothing 是沒有實例的類型** ![](https://box.kancloud.cn/8ef36c1ae43c38eb1b89cdbe6eb67d29_531x126.png) 源碼如下 ~~~ package kotlin /** * Nothing has no instances. You can use Nothing to represent "a value that never exists": for example, * if a function has the return type of Nothing, it means that it never returns (always throws an exception). */ public class Nothing private constructor() ~~~ 從上面的源碼我們可以知道:Nothing是沒有實例的。您可以使用Nothing來表示“一個不存在的值”:例如,如果一個函數的返回類型為Nothing,這意味著它永遠不會返回(總是拋出異常)。 ### Nothing?可以只包含一個值:null Nothing? 它唯一允許的值是 null ,被用作任何可空類型的 空引用。 示例 ![](https://box.kancloud.cn/c56b98efeb26ed26b43215ffd5c6b242_1015x296.png) 綜上所述,我們可以看出Kotlin有一個簡單而一致的類型系統。 Any? 是整個類型體系的頂部, Nothing 是底部。如下圖所示: ![](https://box.kancloud.cn/1529100e1156c6c02397dec2060483c1_814x678.png) ### **PS:備注** 對某些Kotlin 函數來說,"返回類型”的概念沒有任何意義,因為它們從來不會成功地結束。例如, 許多測試庫都有一個叫作fail 的函數,它通過拋出帶有特定消息的異常來讓當前測試失敗。一個包含無限循環的函數也永遠不會成功地結束。 當分析調用這樣函數的代碼時,知道函數永遠不會正常終止是很有幫助的。Kotlin 使用一種特殊的返回類型Nothing 來表示: ``` fun main(args: Array<String>) { fail("Error occurred") } fun fail(message:String):Nothing{ throw IllegalStateException(message) } ``` 運行結果如下 ``` Exception in thread "main" java.lang.IllegalStateException: Error occurred at A基礎.NothingDemoKt.fail(NothingDemo.kt:19) at A基礎.NothingDemoKt.main(NothingDemo.kt:15) ``` Nothing 類型沒有任何值, 只有被當作函數返回值使用,或者被當作泛型函數返回值的類型參數使用才會有意義。在其他所有情況下,聲明一個不能存儲任何值的變量沒有任何意義。 >[info]【注意】,返回Nothing 的函數可以放在Elvis 運算符的右邊來做先決條件檢查: ``` val address =company.address ?: fail("No address") println(address.city) ``` 上面這個例子展示了在類型系統中擁有Nothing 為什么極其有用。編譯器知道這種返回類型的函數從不正常終止, 然后在分析調用這個函數的代碼時利用這個信息。在上面這個例子中,編譯器會把address 的類型推斷成非空,因為它為null 時的分支處理會始終拋出異常。
                  <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>

                              哎呀哎呀视频在线观看