Kotlin 简明教程

Kotlin - Visibility Control (Modifiers)

Kotlin 可见性修饰符是设定类、对象、接口、构造函数、函数以及属性及其 setter 的可见性的关键字。尽管 getter 始终具有与其属性相同的可见性,所以我们不能设定它们的可见性。

The Kotlin visibility modifiers are the keywords that set the visibility of classes, objects, interface, constructors, functions as well as properties and their setters. Though getters always have the same visibility as their properties, so we can not set their visibility.

Kotlin 中有四种可见性修饰符:

There are four visibility modifiers in Kotlin:

  1. public

  2. private

  3. protected

  4. internal

默认可见性是公共的。这些修饰符可以在多个地方使用,例如类头或方法体。让我们详细了解一下这些修饰符:

The default visibility is public. These modifiers can be used at multiple places such as class header or method body. Let’s look into the detail of these modifiers:

Public Modifier

Public 修饰符可以在项目工作空间的任何位置访问。如果未指定访问修饰符,则默认情况下它将在公共范围内。在我们之前的所有示例中,我们没有提到任何修饰符,因此,它们都在公共范围内。以下是一个示例,以进一步了解如何声明公共变量或方法。

Public modifier is accessible from anywhere in the project workspace. If no access modifier is specified, then by default it will be in the public scope. In all our previous examples, we have not mentioned any modifier, hence, all of them are in the public scope. Following is an example to understand more on how to declare a public variable or method.

class publicExample {
   val i = 1

   fun doSomething() {
   }
}

在上面的示例中,我们没有提到任何修饰符,因此此处定义的方法和变量默认为公共的。虽然上面的示例可以通过 public 修饰符明确地写入如下:

In the above example, we have not mentioned any modifier, so the method and variable defined here, are by default public. Though above example can be written with public modifier explicitly as follows:

public class publicExample {
   public val i = 1

   public fun doSomething() {
   }
}

Private Modifier

类、方法、包和其他属性可以用 private 修饰符声明。此修饰符几乎与公共的完全相反,这意味着私有成员不能在其范围外访问。一旦将任何东西声明为私有,则只能在其直接范围内访问它。例如,可以在特定文件中访问私有包。私有类或接口只能由其数据成员等访问。

The classes, methods, packages and other properties can be declared with a private modifier. This modifier has almost the exact opposite meaning of public which means a private member can not be accessed outside of its scope. Once anything is declared as private, then it can be accessible within its immediate scope only. For instance, a private package can be accessible within that specific file. A private class or interface can be accessible only by its data members, etc.

private class privateExample {
   private val i = 1

   private val doSomething() {
   }
}

在上面的示例中,类 privateExample 只可以在同一个源文件中访问,而变量 i 和方法 doSomething 只能在类 privateExample 中访问。

In the above example, the class privateExample is only accessible from within the same source file and the variable i and method doSomething can only be accessed from inside of class privateExample.

Example

我们来看一个简单的示例,说明如何使用私有成员:

Let’s check a simple example showing the usage of private members:

open class A() {
   private val i = 1

   fun doSomething(){
      println("Inside doSomething" )
      println("Value of i is $i" )
   }
}
class B : A() {
   fun printValue(){
       doSomething()
       // println("Value of i is $i" )
   }
}

fun main(args: Array<String>) {
   val a = A()
   val b = B()

   b.printValue()
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Inside doSomething
Value of i is 1

这里我们不能在类 B 中访问变量 i ,因为它已被定义为私有的,这意味着它可以在类本身内部访问,而不能在其他地方访问。

Here we can not access variable i inside class B because it has been defined as private which means it can be accessed inside the class itself and no where else.

Protected Modifier

Protected 是 Kotlin 的另一个访问修饰符,它目前不可用于像任何包一样不能被保护的顶级声明。受保护的类或接口或属性或函数对类本身及其子类可见。

Protected is another access modifier for Kotlin, which is currently not available for top level declaration like any package cannot be protected. A protected class or interface or properties or function is visible to the class itself and it’s subclasses only.

package one;

class A() {
   protected val i = 1
}
class B : A() {
   fun getValue() : Int {
      return i
   }
}

在上面的示例中,变量 i 被声明为受保护的,因此它只能对类本身及其子类可见。

In the above example, the variable i is declared as protected, hence, it is only visible to class itself and it’s subclasses.

Example

我们来看一个简单的示例,说明如何使用受保护成员:

Let’s check a simple example showing the usage of protected members:

open class A() {
   protected val i = 1

   protected fun doSomething(){
      println("Inside doSomething" )
      println("Value of i is $i" )
   }
}
class B : A() {
   fun printValue(){
       doSomething()
       println("Value of i is $i" )
   }
}

fun main(args: Array<String>) {
   val a = A()
   val b = B()

   //a.doSomething()

   b.printValue()
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Inside doSomething
Value of i is 1
Value of i is 1

这里,我们不能在使用类 A 的对象中调用 doSomething() ,因为它已被定义为受保护的,这意味着它只能在类本身或其子类中访问。

Here we can not call doSomething() even using an object of class A because it has been defined as protected which means it can be accessed inside the class itself or in its subclasses only.

Internal Modifier

Internal 是 Kotlin 中新添加的修饰符。如果将任何东西标记为内部,则该特定字段将被标记为内部字段。Internal 包只能在其实现的模块内部可见。内部类接口只能由同一包或模块中存在的其他类看到。在下面的示例中,我们将看到如何实现内部方法。

Internal is a newly added modifier in Kotlin. If anything is marked as internal, then the specific field will marked as the internal field. An Internal package is visible only inside the module under which it is implemented. An internal class interface is visible only by other class present inside the same package or the module. In the following example, we will see how to implement an internal method.

package one

internal class InternalExample {
}

class publicExample{
    internal val i = 1

    internal fun doSomething() {
    }
}

在上面的示例中,类 InternalExample 只能在同一个模块中访问,类似地,变量 i 和函数 doSomething() 也只能在同一个模块中访问,即使类 publicExample 可以从任何地方访问,因为这个类默认情况下具有 public 可见性。

In the above example, class InternalExample is only accessible from inside the same module, similarly variable i and function doSomething() are also accessible from inside the same module only, even though the class publicExample can be accessed from anywhere because this class has public visibility by default.

Example

我们来看一个简单的示例,说明如何使用内部成员:

Let’s check a simple example showing the usage of internal members:

package com.tutorialspoint.modifiers

open class A() {
   internal val i = 1

   internal fun doSomething(){
      println("Inside doSomething" )
      println("Value of i is $i" )
   }
}
class B : A() {
   fun printValue(){
       doSomething()
       println("Value of i is $i" )
   }
}

fun main(args: Array<String>) {
   val a = A()
   val b = B()

   a.doSomething()

   b.printValue()
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Inside doSomething
Value of i is 1
Inside doSomething
Value of i is 1
Value of i is 1