[WIP]Kotlin 可見(jiàn)范圍

Visibility Modifiers
https://kotlinlang.org/docs/reference/visibility-modifiers.html

Visibility Modifiers

Classes, objects, interfaces, constructors, functions, properties and their setters can have visibility modifiers. (Getters always have the same visibility as the property.) There are four visibility modifiers in Kotlin: private, protected, internal and public. The default visibility, used if there is no explicit modifier, is public.

Packages

Functions, properties and classes, objects and interfaces can be declared on the "top-level", i.e. directly inside a package:

// file name: example.kt
package foo

private fun foo() {} // visible inside example.kt

public var bar: Int = 5 // property is visible everywhere
    private set         // setter is visible only in example.kt
    
internal val baz = 6    // visible inside the same module
  • If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere;

  • If you mark a declaration private, it will only be visible inside the file containing the declaration;

  • If you mark it internal, it is visible everywhere in the same module;

  • protected is not available for top-level declarations.

Classes and Interfaces

For members declared inside a class:

  • private means visible inside this class only (including all its members);
  • protected — same as private + visible in subclasses too;
  • internal — any client inside this module who sees the declaring class sees its internal members;
  • public — any client who sees the declaring class sees its public members.
open class Outer {
    private val a = 1
    protected open val b = 2
    internal val c = 3
    val d = 4  // public by default
    
    protected class Nested {
        public val e: Int = 5
    }
}

class Subclass : Outer() {
    // a is not visible
    // b, c and d are visible
    // Nested and e are visible

    override val b = 5   // 'b' is protected
}

class Unrelated(o: Outer) {
    // o.a, o.b are not visible
    // o.c and o.d are visible (same module)
    // Outer.Nested is not visible, and Nested::e is not visible either 
}

Constructors

To specify a visibility of the primary constructor of a class, use the following syntax (note that you need to add an explicit constructor keyword):

class C private constructor(a: Int) { ... }

Local declarations

Local variables, functions and classes can not have visibility modifiers.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,840評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,037評(píng)論 0 23
  • 你曾許我一生一世 我愿化蝶 生生死死永不分離。 “小姐,小姐”,秋兒似乎望見(jiàn)無(wú)盡的輪回,燦若煙花,微微笑著,是如此...
    唯美成殤閱讀 882評(píng)論 1 1
  • 三月艷陽(yáng)殷風(fēng)暖 煙柳堤下枕石眠 萬(wàn)里花田曾遺劍 不知何人何時(shí)撿 附送滿眼春光色 唯恐有我無(wú)人看 捧酒揚(yáng)歌理長(zhǎng)發(fā) 化...
    林映澈閱讀 244評(píng)論 3 1
  • 今天早上完英語(yǔ)課,女兒打電話,說(shuō)讓晚一個(gè)小時(shí)去接她,他們?cè)趯W(xué)習(xí)班寫會(huì)兒作業(yè),寫完作業(yè)再去接她,我聽(tīng)了很開(kāi)心,現(xiàn)在孩...
    可樂(lè)開(kāi)心閱讀 143評(píng)論 4 6

友情鏈接更多精彩內(nèi)容