如何在 Kotlin 中同时进行扩展和实现?\n


在这篇文章中,我们将通过一个示例演示如何在同一类中扩展和实现。在这个示例中,

  • 我们将创建一个接口和一个虚拟父类。

  • 从子类中,我们将扩展父类并实现该接口。

示例 - 在子类中扩展和实现

Open Compiler
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

更新于: 01-Mar-2022

613 次查看

开启你的 职业

通过完成课程获得认证

开始学习
广告