kotlin 程序入口_Kotlin程序| 各种可见性修改器的示例

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,

    可见性修改器有四种类型,

    1. Public
    2. Private
    3. Protected
    4. Internal
  • Getters always have the same visibility as the properties.

    吸气剂始终具有与属性相同的可见性。

  • Visibility modifiers for members declared inside class and interfaces,

    在类和接口内部声明的成员的可见性修改器,

    1. publicpublic
    2. privateprivate
    3. protectedprotected
    4. 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 程序入口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值