Kotlin教程:面向对象之继承与实现

2021-01-3118:32:59编程语言入门到精通Comments1,754 views字数 3157阅读模式

继承是面向对象编程的三大特性之一,在开发过程中会经常使用,继承可以让子类拥有父类的功能,也可以对父类功能进行增强修改。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

面向对象编程的三大特性:封装、继承、多态文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

继承普通类

Kotlin 中默认 类、方法 都是 final 的,因此默认无法被子类继承或重写,但可通过 open 关键字来解除:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

  • 父类需要 open 才可以被继承
  • 父类方法、属性需要 open 才可以被覆写
  • 覆写父类成员需要 override 关键字
open class Person() {
    open fun walk() {
        println("慢慢走")
    }
}

class Doctor : Person() {
    override fun walk() {
        // super.walk()
        println("快走")
    }
}
复制代码

子类重写父类方法时,会使用到 override 关键字,另外,重写的方法体中可以使用 super.xxx() 来调用父类方法中原有的逻辑,当然你也可以选择完全重写(方法体中不使用 super.xxx() 即可)。Kotlin 中父类的属性与方法一样,子类要重写的话,需要使用 open 关键字修饰父类属性,解除 final:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

// 重写父类属性
open class Person(open val name: String) {
    ...
}
class Doctor(override val name: String) : Person(name) {
    ...
}
复制代码

当然啦,也不是所有子类都需要重写父类成员属性的,因为子类可通过 getter/setter 对父类的成员属性进行访问。这时子类仅需要将临时变量(即不使用 val 或 var 修饰的构造器参数变量)传递到父类构造器中就好了:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

  • 继承类时实际上是调用了父类构造方法
// 传递构造参数
open class Person(val name: String) { // 使用了val,所以name是父类Person的成员属性。
    ...
}
class Doctor(name: String) : Person(name) { // 没有val,所以name仅仅只是一个临时变量。
    ...
}

// 不想重写父亲属性就不要使用val或var修改,因为这相当于在子类中定义了一个与父类成员属性同名的属性。
// class Doctor(val name: String) : Person(name) // IDE报错:'name' hides memeber of supertype 'Person' and needs 'override' modifier
复制代码

继承(实现)抽象类(接口)

相比于普通类,抽象类与接口就比较"开放"了,因为它们本身设计出来就是为了让子类(实现类)去继承(实现)的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

  • 接口、接口方法、抽象类默认为 open
abstract class Person() {
    open fun walk() { // 成员方法,想被子类重写需要使用open关键字
        println("慢慢走")
    }

    abstract fun work() // 抽象方法,默认就是open的
}

class Doctor : Person() {
    override fun walk() {
        println("快走")
    }

    override fun work() { // 子类实现父类抽象方法,还是需要使用override关键字
    }
}
复制代码

接口代理

Kotlin 不支持多继承,只能使用接口来实现多个功能,但我们又不想在类中将接口方法的实现写死,这样代码就不够灵活,因此,往往会在类的构造器参数中,将接口的实现类传进来,而在我们的类中,只需要在实现了接口方法的方法体中调用实现类的对应方法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

interface Driver {
    fun dirve()
}

class Manager(val driver: Driver) : Driver {
    override fun dirve() {
        driver.dirve()
    }
}
复制代码

但这种写法会显得比较罗嗦,Kotlin 提供了 接口代理 将接口方法实现直接交给代理类实现:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

  • 格式: class XXX(接口实现类) : 接口 by 接口实现类
class Manager(driver: Driver): Driver by driver
复制代码

这算不算是对 Kotlin 不支持多继承的 "曲线救国" 呢?其实 Java 也一样不支持多继承,通常会使用组合的方式来处理。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

接口方法冲突

前面我们已经知道了接口方法是可以有默认实现的,Kotlin 可以实现多个接口,而这些接口可能拥有相同的接口方法,就比如这样:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

interface Radio {
    fun display(): String {
        return "107.1"
    }
    ...
}

interface Compass {
    fun display(): String {
        return "东方"
    }
    ...
}
复制代码

这时有一个实现类,同时实现了上述两个接口,就会出现接口方法冲突问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

class Phone(val type: Int) : Radio, Compass {
    override fun display(): String {
        return super.display() // IDE报错:Many supertypes available, please specify the one you mean in angle brackets, e.g. 'super<Foo>'
    }
    ...
}
复制代码

根据提示,这个接口方法冲突问题其实也挺好解决的,只需要在 super 后使用泛型指定父类(接口)名即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

class Phone(val type: Int) : Radio, Compass {
    override fun display(): String {
        when (type) {
            0 -> return super<Radio>.display()
            1 -> return super<Compass>.display()
            else -> return "不支持"
        }
    }
}
复制代码

要注意,这里的接口方法冲突,指的是 签名一致且返回值相同的冲突 ,如果签名一致但返回值类型不同,那这个问题将无解:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

方法签名一致指的是方法名、参数列表相同。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

interface Radio {
    fun display(): String {
        return "107.1"
    }
}

interface Compass {
    fun display(): Int {
        return 180
    }
}

// IDE报错:
// Platform declaration clash: The following declarations have the same JVM signature (display()Ljava/lang/String;)
// Platform declaration clash: The following declarations have the same JVM signature (display()I;)
class Phone(val type: Int) : Radio, Compass {
    override fun display(): String { // IDE报错
    }

    override fun display(): Int { // IDE报错
    }
}
复制代码

但反过来,如果方法返回值相同但签名不一致,那这情况就不一样了,就相当于 2 个不一样的方法,需要分开重写,只是在使用 super.xxx() 调用接口方法时同样还是需要使用泛型来指明父接口,避免发生歧义:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html


interface Radio {
    fun display(): String {
        return "107.1"
    }
}

interface Compass {
    fun display(i: Int): Int {
        return i
    }
}

class Phone(val type: Int) : Radio, Compass {
    override fun display(): String {
        return super <Radio>.display()
    }

    override fun display(i: Int): Int {
        return super<Compass>.display(i)
    }
}

作者:GitLqr文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/20906.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/20906.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定