kotlin - 使用 super 的 kotlin 中的扩展函数

标签 kotlin kotlin-extension

如何使用 super 关键字在派生类中调用基类的扩展函数?

我尝试使用 super 调用,但它不起作用。

 open class abc {
     open fun aa() {
         println("function in abc")
     }
 }
 fun abc.sum() {
     println("extension function")
 }
 class ab: abc() {

     override fun aa() {
         super.aa()
         println("functon in ab")
     }
     fun sum() {
         super.sum()
         println("sum function")
     }
 }
 fun main(args: Array < String > ) {
     var aa: ab = ab()
     aa.aa()
     aa.aa()
     aa.sum()
 }

这里是第 16 个数字行错误来了,我无法调用扩展功能。

最佳答案

这将做:

(this as abc).sum()
super不起作用,因为扩展函数不是要在派生类中覆盖的基类的成员。

关于kotlin - 使用 super 的 kotlin 中的扩展函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51170152/

相关文章:

android - 扩展函数上的协程

android - 在 Android 应用中声明 Kotlin 扩展函数的位置

gradle - 如何在库jar中打包扩展功能?

Kotlin + Gson - 如何在数据类为 null 时获取空列表

android - 使用测验应用程序部署问题

javafx-2 - Kotlin 中的 TableView 回调

android - 使用方法扩展和继承时名称冲突

Kotlin 扩展方法作为长方法名称的别名?

android - ViewModel中的LiveData类型不匹配

oracle - 在 Spring Data Repository 中刷新 Oracle Materialized View