Kotlin 接口 interface 默认实现了open。并且可以提供默认实现
原创
©著作权归作者所有:来自51CTO博客作者mp624183768的原创作品,请联系作者获取转载授权,否则将追究法律责任
interface Movable {
var maxSpeed: Int
var wheels: Int
fun move(movable: Movable): String
}
class Car(_name: String, override var wheels: Int = 4) : Movable {
override var maxSpeed: Int
get() = 1
set(value) {}
override fun move(movable: Movable): String {
}
}
Kotlin规定。所有的接口属性和函数都要使用override关键字。接口中定义的函数并不需要open关键字修饰。他们默认就是open的
默认实现
interface Movable {
val maxSpeed: Int
get() = (1..500).random()
var wheels: Int
fun move(movable: Movable): String
}
class Car(_name: String, override var wheels: Int = 4) : Movable {
override var maxSpeed: Int
get() = super.maxSpeed
set(value) {}
override fun move(movable: Movable): String {
return "";
}
}
fun main() {
(1..100).forEach {
println((1..3).random())
}
}
和Java不同。kotlin的接口可以实现默认参数。