[Kotlin] Overriding Rules (super, final)

import java.util.*

/**
 * You can edit, run, and share this code. 
 * play.kotlinlang.org 
 */

abstract class Course(val topic: String, val price: Double) {
    open fun learn() {
        println("Learnign a $topic course")
    }
}

interface Readable {
    fun read() {
        println("Reading..")
    }
}

class KotlinCourse(): Course("Kotlin", 99.99), Readable {
    // final: don't allow child class to override learn()
    override final fun learn() {
        super<Course>.learn()
        super<Readable>.read()
        println("I want to learn Kotlin")
    }
    
    
    override fun read() {
        println("Leanring the kotlin")
    }
}


fun main() {
    val course = KotlinCourse()
    course.learn()
}

 

上一篇:低代码+RPA+AI,能否让ERP焕发下一春?


下一篇:c – 如何在QFileSystemModel中覆盖(重新实现)成员函数