[WIP]Kotlin 可见范围

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.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,205评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,609评论 0 23
  • 你曾许我一生一世 我愿化蝶 生生死死永不分离。 “小姐,小姐”,秋儿似乎望见无尽的轮回,灿若烟花,微微笑着,是如此...
    唯美成殇阅读 4,272评论 1 1
  • 三月艳阳殷风暖 烟柳堤下枕石眠 万里花田曾遗剑 不知何人何时捡 附送满眼春光色 唯恐有我无人看 捧酒扬歌理长发 化...
    林映澈阅读 1,643评论 3 1
  • 今天早上完英语课,女儿打电话,说让晚一个小时去接她,他们在学习班写会儿作业,写完作业再去接她,我听了很开心,现在孩...
    可乐开心阅读 1,180评论 4 6

友情链接更多精彩内容