如何在 Kotlin 中同時進行擴充套件和實現?\n
在這篇文章中,我們將透過一個示例演示如何在同一類中擴充套件和實現。在這個示例中,
我們將建立一個介面和一個虛擬父類。
從子類中,我們將擴充套件父類並實現該介面。
示例 - 在子類中擴充套件和實現
interface myInterface { fun test(): String } open class myParentClass(val name:String) { fun anotherTest():String { return name } } class Child() : myInterface, myParentClass("ParentClass Input"){ //child extending the parent class and implementing myInterface override fun test(): String { return "This is implemented interface: myInterface" } } fun main(args : Array<String>) { println(Child().test()) println("Reply from parent entity: "+myParentClass("hello parent").anotherTest()) }
輸出
一旦你執行程式碼,它將產生以下輸出 −
This is implemented interface: myInterface Reply from parent entity: hello parent
廣告