前言
Kotlin的继承基本上跟java的类似。
Kotlin强化了接口实现接口的逻辑。可以更清晰的呈现 每个接口类的描述。
除了文字版本,也有Xmind版本 github地址
定义
使用 interface 定义
范例
interface MyInterface {
fun bar() // 未实现
fun foo() { //已实现
// 可选的方法体
println("foo")
}
}
接口中的属性
只能是抽象的,不允许初始化值。
在接口中声明的属性
- 要么是抽象的
- 要么提供访问器的实现
在接口中声明的属性不能有幕后字段(backing field)
实现接口时,必须重写属性。
范例
interface MyInterface {
val prop: Int // 抽象的
val propertyWithImplementation: String
get() = "foo"
fun foo() {
print(prop)
}
}
class Child : MyInterface {
overri