📜  Kotlin 可见性修改器

📅  最后修改于: 2022-05-13 01:54:18.951000             🧑  作者: Mango

Kotlin 可见性修改器

在 Kotlin 中,可见性修饰符用于将类、对象、接口、构造函数、函数、属性及其设置器的可访问性限制在一定水平。无需设置 getter 的可见性,因为它们与属性具有相同的可见性。
Kotlin 中有四个可见性修饰符。

如果没有指定修饰符,则默认为public 。让我们开始一一讨论上述修饰符。

1.公共修饰符

在 Kotlin 中,默认修饰符是public 。它可能是整个语言中最常用的修饰符,并且对谁可以看到被修改的元素有额外的限制。与Java不同,在 Kotlin 中不需要将任何东西声明为public ——它是默认修饰符,如果我们不声明另一个修饰符——public在 Kotlin 中的工作方式与在Java中相同。当我们将public修饰符应用于顶级元素——直接在包中声明的类、函数或变量时,任何其他代码都可以访问它。如果我们将public修饰符应用于嵌套元素——内部类或类中的函数——那么任何可以访问容器的代码也可以访问这个元素。

Kotlin
// by default public
class A {            
    var int = 10
}
 
// specified with public modifier
public class B {    
    var int2 = 20
    fun display() { 
    println("Accessible everywhere")        
    }
}


Kotlin
// class A is accessible from same source file
private class A {
    private val int = 10
    fun display()
    {
        // we can access int in the same class
        println(int)  
        println("Accessing int successful")
    }
}
fun main(args: Array){
    var a = A()
    a.display()
    // can not access 'int': it is private in class A
    println(a.int) 
}


Kotlin
internal class A {
}
public class B {
    internal val int = 10
    internal fun display() {
    }
}


Kotlin
// base class
open class A {
      // protected variable
    protected val int = 10
}
 
// derived class
class B: A() {
    fun getvalue(): Int {
          // accessed from the subclass
        return int        
    }
}
 
fun main(args: Array) {
    var a = B()
    println("The value of integer is: "+a.getvalue())
}


Kotlin
// base class
open class A {
     // protected variable
    open protected val int = 10
 
}
 
// derived class
class B: A() {
   override val int = 20
    fun getvalue():Int {
          // accessed from the subclass
        return int        
    }
}
 
fun main(args: Array) {
    var a = B()
    println("The overridden value of integer is: "+a.getvalue())
}


在这里,可以从整个代码中的任何位置访问 A 类和 B 类,变量intint2 并且函数display()可以从任何可以访问类 A 和 B 的东西访问。

2.私人修饰符

在 Kotlin 中,私有修饰符只允许在同一范围内声明的代码访问。它不允许访问范围之外的修饰符变量或函数。与Java不同,Kotlin 允许在同一个文件中声明多个顶级声明——私有顶级元素可以被同一个文件中的所有其他内容访问。

科特林

// class A is accessible from same source file
private class A {
    private val int = 10
    fun display()
    {
        // we can access int in the same class
        println(int)  
        println("Accessing int successful")
    }
}
fun main(args: Array){
    var a = A()
    a.display()
    // can not access 'int': it is private in class A
    println(a.int) 
}

输出:

Cannot access 'int': it is private in 'A'

在这里,类 A 只能从同一个源文件中访问,而int变量只能从类 A 内部访问。当我们尝试从类外部访问int时,会出现编译时错误。

3. 内部修饰符

在 Kotlin 中, internal修饰符是Java不支持的新增修饰符。标记为内部意味着它将在同一个模块中可用,如果我们尝试从另一个模块访问声明,它将给出错误。模块是指一起编译的一组文件。

科特林

internal class A {
}
public class B {
    internal val int = 10
    internal fun display() {
    }
}

在这里,只能从同一模块内部访问 A 类。变量int和函数display()只能从同一个模块内部访问,即使 B 类可以从任何地方访问。

4. 受保护的修饰符

在 Kotlin 中, protected修饰符严格允许访问声明类及其子类。 protected 修饰符不能在顶层声明。在下面的程序中,我们访问了派生类的getvalue()函数中的 int 变量。

科特林

// base class
open class A {
      // protected variable
    protected val int = 10
}
 
// derived class
class B: A() {
    fun getvalue(): Int {
          // accessed from the subclass
        return int        
    }
}
 
fun main(args: Array) {
    var a = B()
    println("The value of integer is: "+a.getvalue())
}

输出:

The value of integer is: 10

覆盖受保护的修饰符

我们需要使用open关键字标记受保护的变量或函数,以便在派生类中覆盖。在下面的程序中,我们覆盖了int变量。

科特林

// base class
open class A {
     // protected variable
    open protected val int = 10
 
}
 
// derived class
class B: A() {
   override val int = 20
    fun getvalue():Int {
          // accessed from the subclass
        return int        
    }
}
 
fun main(args: Array) {
    var a = B()
    println("The overridden value of integer is: "+a.getvalue())
}

输出:

The value of integer is: 20

构造函数可见性

默认构造函数是public ,但我们也可以通过使用修饰符来改变构造函数的可见性。

class A (name : String) {
      // other code
}

我们必须在更改可见性的同时使用构造函数关键字明确指定这一点。

class A private constructor (name : String) {
      // other code
}