Kotlin_接口(interface)

接口

package com.tct.freeze.kotlinstudydemo

// interface 接口
interface People {
    // 默認方法
    fun onFavourite(){
        println("Study")
    }

    // 不實現默認方法
    fun onPrint()
}

類實現接口

package com.tct.freeze.kotlinstudydemo

// 類實現接口
class Student : People {

    override fun onPrint() {
        println("I am a Student")
    }

    override fun onFavourite() {
        super.onFavourite()
    }

}

調用

package com.tct.freeze.kotlinstudydemo


fun main(args : Array<String>) {
    var mStudent = Student()
    mStudent.onPrint()
    mStudent.onFavourite()
}

運行結果

I am a Student
Study

Process finished with exit code 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章