如何在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP