当实现多个具有相同名称的方法(包括默认实现)的接口时,对于编译器来说应该使用哪个实现是不明确的。在发生冲突的情况下,开发人员必须重写冲突的方法并提供自定义实现。该实现可以选择委托或不委托给默认实现。
interface FirstTrait {
fun foo() { print("first") }
fun bar()
}
interface SecondTrait {
fun foo() { print("second") }
fun bar() { print("bar") }
}
class ClassWithConflict : FirstTrait, SecondTrait {
override fun foo() {
super<FirstTrait>.foo() // 委托给FirstTrait的默认实现
super<SecondTrait>.foo() // 委托给SecondTrait的默认实现
}
// 功能bar()仅在一个接口中具有默认实现,因此可以。
}