kotlin 程序入口
可见性修改器 (Visibility Modifiers)
Visibility modifiers are that set the accessibility (visibility) of Classes, objects, interfaces, constructors, functions, properties, and their setters.
可见性修改器用于设置类,对象,接口,构造函数,函数,属性及其设置器的可访问性(可见性)。
There are four types of visibility modifiers,
可见性修改器有四种类型,
- Public
- Private
- Protected
- Internal
Getters always have the same visibility as the properties.
吸气剂始终具有与属性相同的可见性。
Visibility modifiers for members declared inside class and interfaces,
在类和接口内部声明的成员的可见性修改器,
- publicpublic
- privateprivate
- protectedprotected
- internalinternal
Local variables, functions, and classes can not have visibility modifiers.
局部变量,函数和类不能具有可见性修饰符。
To specify the visibility of the primary constructor of a class, use the following syntax (by default constructors are public),
要指定类的主要构造函数的可见性,请使用以下语法(默认情况下,构造函数是公共的),
class C private constructor(a: Int) { ... }
程序,以演示Kotlin中的各种可见性修改器的示例 (Program to demonstrate the example of Various Visibility Modifiers in Kotlin)
package com.includehelp
// Class, by default public visibility
// Mark with 'open' to make inheritable
open class MyOuter{
private var myname="IncludeHelp India !!"
protected open val age=30
internal var salary=100
var city="Delhi" // Public , by default
protected fun fun1(){
println("Protected Function in Base !!")
}
}
class MySub: MyOuter() {
// Override protected members,
// because protected is visible in subclass
override val age: Int
get() = 50
fun printDetails(){
// Can't Access $myname as it is private in super class
// println("Name : $myname")
println("City : $city")
println("Salary : $salary")
println("Age : $age")
}
}
// Main function, Entry Point of Program
fun main(){
// Create Instance
val myOuter = MySub()
// Call subclass method
myOuter.printDetails()
}
Output:
输出:
City : Delhi
Salary : 100
Age : 50
翻译自: https://www.includehelp.com/kotlin/example-of-various-visibility-modifiers.aspx
kotlin 程序入口