kotlin可见性关键字

在kotlin文件中修饰方法,默认为public,不能用protected

private fun test1() {}
internal fun test3() {}
// public 为默认
public fun test4() {}
// protected会报错
protected fun test2() {}

fun main(args:ArrayList<String>){
    test1()
    test3()
    test4()
}

修饰属性与修饰方法一样
在类中也一样

class Privage{
    public var num1 =1
    public fun test1(){}

    private var num2 =1
    private fun test2(){}

    protected var num3 =1
    protected fun test3(){}

    internal var num4 =1
    internal fun test4(){}
}

一个类要想被继承,必须用open修饰,方法要想重写也必须加open修饰

// 需要加open
open class Privage{
    public var num1 =1
    
    // 需要加open
    open public fun test1(){}
}

class son:Privage(){
    fun test(){
        test1()
        num1=3
    }

    override fun test1() {
        super.test1()
    }
}

接口和抽象类的方法默认是open的

interface test {
    // 默认open
    fun test1()
    // 默认open
    public fun test2()
}

abstract class abclass{
    // 默认open
    abstract fun test3()
}

class son : abclass(),test {
    override fun test3() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
    
    override fun test1() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun test2() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值