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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Kotlin 嵌套和內部類 > 原文: [https://www.programiz.com/kotlin-programming/inner-nested-class](https://www.programiz.com/kotlin-programming/inner-nested-class) #### 在本文中,您將借助示例學習使用嵌套類和內部類。 ## Kotlin 嵌套類 與 Java 類似,Kotlin 允許您在另一個稱為嵌套類的類中定義一個類。 ```kt class Outer { ... .. ... class Nested { ... .. ... } } ``` 由于`Nested`類是其封閉類`Outer`的成員,因此可以使用`.`表示法訪問`Nested`類及其成員。 * * * ### 示例:Kotlin 嵌套類 ```kt class Outer { val a = "Outside Nested class." class Nested { val b = "Inside Nested class." fun callMe() = "Function call from inside Nested class." } } fun main(args: Array<String>) { // accessing member of Nested class println(Outer.Nested().b) // creating object of Nested class val nested = Outer.Nested() println(nested.callMe()) } ``` 運行該程序時,輸出為: ```kt Inside Nested class. Function call from inside Nested class. ``` * * * **適用于 Java 用戶** Kotlin 中的嵌套類類似于 Java 中的靜態嵌套類。 在 Java 中,當您在另一個類中聲明一個類時,默認情況下它將成為一個內部類。 但是在 Kotlin 中,您需要使用`inner`修飾符來創建一個內部類,我們將在下面討論。 * * * ## Kotlin 內部類 Kotlin 中的嵌套類無法訪問外部類實例。 例如, ```kt class Outer { val foo = "Outside Nested class." class Nested { // Error! cannot access member of outer class. fun callMe() = foo } } fun main(args: Array<String>) { val outer = Outer() println(outer.Nested().callMe()) } ``` 上面的代碼無法編譯,因為我們嘗試從`Nested`類內部訪問`Outer`類的`foo`屬性。 為了解決此問題,您需要使用`inner`標記嵌套的類以創建內部類。 內部類帶有對外部類的引用,并且可以訪問外部類成員。 * * * ### 示例:Kotlin 內部類 ```kt class Outer { val a = "Outside Nested class." inner class Inner { fun callMe() = a } } fun main(args: Array<String>) { val outer = Outer() println("Using outer object: ${outer.Inner().callMe()}") val inner = Outer().Inner() println("Using inner object: ${inner.callMe()}") } ``` 運行該程序時,輸出為: ```kt Using outer object: Outside Nested class. Using inner object: Outside Nested class. ``` * * * **推薦閱讀**: *匿名內部類*
                  <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>

                              哎呀哎呀视频在线观看