Kotlin:我如何称之为超级扩展功能?

时间:2017-06-02 03:51:05

标签: kotlin extension-methods extension-function

如何调用超级扩展功能?

例如:

{
    "Destination": {
        "BccAddresses": [],
        "CcAddresses": [],
        "ToAddresses": [
            "success@simulator.amazonses.com"
        ]
    },
    "Message": {
        "Body": {
            "Html": {
                "Charset": "UTF-8",
                "Data": "You have been removed from Kudo mailing list for account: bob@gmail.com"
            },
            "Text": {
                "Charset": "UTF-8",
                "Data": "You have been removed from Kudo mailing list for account: bob@gmail.com"
            }
        },
        "Subject": {
            "Charset": "UTF-8",
            "Data": "Kudo email removal"
        }
    },
    "ReplyToAddresses": [],
    "ReturnPath": "",
    "ReturnPathArn": "",
    "Source": "donotreply@kudo.io",
    "SourceArn": "arn:aws:ses:us-west-2:1xxxxxxxxxx2:identity/donotreply@kudo.io"
}

2 个答案:

答案 0 :(得分:2)

即使在print()内定义Parent函数,它也属于String,而不属于Parent。因此,您可以在print上调用Parent功能,这是您尝试对super进行的操作。

我不认为您在Kotlin尝试做的电话类型有语法支持。

答案 1 :(得分:0)

目前不可能,而且 kotlin 问题跟踪器中存在问题 - KT-11488

但您可以使用以下解决方法:

open class Parent {
    open fun String.print() = parentPrint()

    // Declare separated parent print method
    protected fun String.parentPrint() = println(this)
}

class Child : Parent() {
    override fun String.print() {
        print("child says ")
        parentPrint() // <-- Call parent print here
    }
}