在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.
}
}