<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國際加速解決方案。 廣告
                # Kotlin 可見性修飾符 > 原文: [https://www.programiz.com/kotlin-programming/visibility-modifiers](https://www.programiz.com/kotlin-programming/visibility-modifiers) #### 在本文中,您將了解 Kotlin 中的所有 4 種可見性修飾符,以及它們在不同情況下的工作方式。 可見性修飾符是用于設置類,對象,接口,構造器,函數,屬性及其設置程序的可見性(可見性)的關鍵字。 (您無法設置獲取器的可見性修飾符,因為它們始終具有與屬性相同的可見性。) 在 [Kotlin 類和對象](/kotlin-programming/class-objects "Kotlin Class and Objects")文章中,您簡要了解了可見性修飾符`public`和`private`。 您將詳細了解另外兩個可見性修飾符`protected`和`internal`(以及`public`和`private`)。 * * * ## 包內的可見性修飾符 程序包組織了一組相關的函數,屬性和類,對象和接口。 **推薦閱讀**: *Kotlin 包* | 編輯 | 描述 | | --- | --- | | `public` | 聲明隨處可見 | | `private` | 在包含聲明的文件內可見 | | `internal` | 在同一模塊內可見(一組一起編譯的 Kotlin 文件) | | `protected` | 不適用于包(用于子類) | **注意**:如果未指定可見性修飾符,則默認為`public`。 讓我們舉個例子: ```kt // file name: hello.kt package test fun function1() {} // public by default and visible everywhere private fun function2() {} // visible inside hello.kt internal fun function3() {} // visible inside the same module var name = "Foo" // visible everywhere get() = field // visible inside hello.kt (same as its property) private set(value) { // visible inside hello.kt field = value } private class class1 {} // visible inside hello.kt ``` * * * ## 類和接口內的可見性修飾符 這是可見性修飾符對在類內聲明的成員(函數,屬性)的工作方式: | 修飾符 | 描述 | | --- | --- | | `public` | 任何可以看到聲明類的客戶都可以看到 | | `private` | 僅在類內部可見 | | `protected` | 在類及其子類中可見 | | `internal` | 對于模塊內可以看到聲明類的任何客戶端可見 | **注意**:如果在派生類中覆蓋`protected`成員而未指定其可見性,則其可見性也將為`protected`。 Let's take an example: ```kt open class Base() { var a = 1 // public by default private var b = 2 // private to Base class protected open val c = 3 // visible to the Base and the Derived class internal val d = 4 // visible inside the same module protected fun e() { } // visible to the Base and the Derived class } class Derived: Base() { // a, c, d, and e() of the Base class are visible // b is not visible override val c = 9 // c is protected } fun main(args: Array<String>) { val base = Base() // base.a and base.d are visible // base.b, base.c and base.e() are not visible val derived = Derived() // derived.c is not visible } ``` * * * ## 更改構造器的可見性 默認情況下,構造器的可見性為`public`。 但是,您可以更改它。 為此,您需要顯式添加`constructor`關鍵字。 在下面的示例中,構造器默認為`public`: ```kt class Test(val a: Int) { // code } ``` 您可以通過以下方法更改其可見性。 ```kt class Test private constructor(val a: Int) { // code } ``` 這里的構造器是`private`。 * * * **注意**:在 Kotlin 中,局部函數,變量和類不能具有可見性修飾符。
                  <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>

                              哎呀哎呀视频在线观看