kotlin 接口继承
接口内的继承 (Inheritance within Interface)
An Interface Derive from other interfaces.
从其他接口派生的接口。
The derived interface can override super interface members or declare new functions and properties
派生的接口可以覆盖超级接口成员或声明新的函数和属性
Kotlin class implementing such an interface are only need to define missing implementations
实现此类接口的Kotlin类仅需要定义缺少的实现
程序演示了Kotlin中接口内继承的示例 (Program demonstrate the example of Inheritance within Interface in Kotlin)
package com.includehelp
// Declare Interface
interface AA{
// Abstract property
val count:Int
// Abstract methods
fun sayHello()
// Method with implementation
fun sayGudMorning(){
println("Gud Morning, IncludeHelp !!")
}
}
// Derived interface from another super interface
interface BB:AA{
// Abstract methods
fun sayBye()
// Override Interface abstract methods,
// declare into super interface
override fun sayHello() {
println("Hello, IncludeHelp !!")
}
}
// Declare class implementing interface
class IncludeHelp:BB{
// Override abstract property
override val count: Int
get() = 100
// Override Interface abstract methods
override fun sayBye() {
println("Gud Bye, IncludeHelp !!")
}
}
// Main function, entry point of program
fun main(){
// Create instance of class
val includeHelp = IncludeHelp()
// Call method
includeHelp.sayGudMorning()
// Call method
includeHelp.sayHello()
// Call method
includeHelp.sayBye()
println("Abstract Property : ${includeHelp.count}")
}
Output:
输出:
Gud Morning, IncludeHelp !!
Hello, IncludeHelp !!
Gud Bye, IncludeHelp !!
Abstract Property : 100
翻译自: https://www.includehelp.com/kotlin/example-of-inheritance-within-interface.aspx
kotlin 接口继承