Kotlin接口与抽象类

本文最后更新于:2022年11月30日 晚上

接口

接口定义

Kotlin规定所有的接口属性和函数实现都要使用override关键字,接口中定义的函数并不需要open关键字修饰,他们默认就是open的。

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() = TODO("Not yet implemented")
        set(value) {}

    override fun move(movable: Movable): String {
        TODO("Not yet implemented")
    }
}

默认实现

可以在接口里提供默认属性的getter方法和函数实现。

interface Movable {
    var maxSpeed: Int
        get() = (1..500).shuffled().last()
        set(value) {}
    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 {
        TODO("Not yet implemented")
    }
}

抽象类

要定义一个抽象类,你需要在定义之前加上abstract关键字,除了具体的函数实现,抽象类也可以包含抽象函数—只有定义,没有函数实现。

abstract class Gun(val range: Int) {
    protected fun doSomething() {
        println("doSomething")
    }

    abstract fun pullTrigger(): String
}

class M416(val price: Int) : Gun(range = 600) {
    override fun pullTrigger(): String {
        TODO("Not yet implemented")
    }
}

Kotlin接口与抽象类
http://yorick.love/2022/09/12/Kotlin/Kotlin接口与抽象类/
作者
Yorick
发布于
2022年9月12日
许可协议