如何使用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()
}
Run Code Online (Sandbox Code Playgroud)
这是16号数字错误,我无法调用扩展功能。
是否可以做类似的事情:
/**
* Converts all of the characters in the string to upper case.
*
* @param str the string to be converted to uppercase
* @return the string converted to uppercase or empty string if the input was null
*/
fun String?.toUpperCase(): String = this?.toUpperCase() ?: ""
Run Code Online (Sandbox Code Playgroud)
toUpperCasenull安全。this?.toUpperCase()表示扩展功能是重命名扩展功能的唯一选择,还是可以从其中引用“超级”功能?