接口继承了之后,我明明在子接口中实现了父接口的方法,为啥实现子接口,依然要求必须要实现父接口方法,不实现就不行呢?如果是这样设计的话,那接口继承的目的是什么?
trait Trait1{
trait1Value(Self)->Int
}
trait Trait2: Trait1{
trait1(Self)->Trait1
trait2Value(Self)->Int
}
impl Trait1 for Trait2 with trait1Value(self)->Int{
self.trait1().trait1Value()
}
struct TestData1{
x: Int
}
impl Trait1 for TestData1 with trait1Value(self)->Int{
self.x + 30
}
struct TestData2{
data1: TestData1
data2: Int
}
impl Trait2 for TestData2 with trait2Value(self)->Int{
self.data1.x + self.data2
}
impl Trait2 for TestData2 with trait1(self)->Trait1{
self.data1
}
//为啥这个必须要实现,Trait2接口明明已经实现了Trait1接口的方法?
// impl Trait1 for TestData2 with trait1Value(self)->Int{
// self.data1.x + 50
// }
fn main {
let t1: TestData1={x: 30}
if Trait1::trait1Value(t1) == 60{
println("OK")
}
let t2: TestData2={
data1: t1,
data2: 40
}
let _= Trait1::trait1Value(t2)
let _= Trait2::trait2Value(t2)
println(@lib.hello())
}