super
成员调用。
派生类中的代码可以使用 super
关键字调用其超类函数和属性访问器实现。
要指定从中获取继承实现的超类型,可以通过尖括号中的超类型名称来限定 super
,例如 super<Base>
。 有时这种限定是冗余的,可以省略。
使用“移除显式父类型限定”快速修复来清理代码。
示例:
open class B {
open fun foo(){}
}
class A : B() {
override fun foo() {
super<B>.foo() // <== redundant because 'B' is the only supertype
}
}
interface I {
fun foo() {}
}
class C : B(), I {
override fun foo() {
super<B>.foo() // <== here <B> qualifier is needed to distinguish 'B.foo()' from 'I.foo()'
}
}
在应用快速修复后:
open class B {
open fun foo(){}
}
class A : B() {
override fun foo() {
super.foo() // <== Updated
}
}
interface I {
fun foo() {}
}
class C : B(), I {
override fun foo() {
super<B>.foo()
}
}