interface B{
fun x() : Int = 1
}
interface C{
fun x() : Int = 0
}
open class A{
fun x() : Int = 0
}
class D (val y : Int): A(),B,C{
}
如上這段代碼党觅,類(lèi)D繼承A實(shí)現(xiàn)B,C會(huì)造成函數(shù)x重寫(xiě)時(shí)候的沖突問(wèn)題
class D (val y : Int): A(),B,C{
//這里我并不知道到底是A,B,C中的哪個(gè)x()
override fun x() : Int{
}
}
所以我們需要解決這種情況通過(guò)如下方法
class D (val y : Int): A(),B,C{
override fun x() : Int{
if (y > 0){
return y;
}else if (y < 0){
return super<B>.x();
}else if (y < -100){
return super<C>.x();
}else{
return super<A>.x();
}
}
}