考虑这个(相当乏味的)代码:
class SCell : NSObject {}
class SHeader : NSObject {}
class Cell : SCell {}
class Header : SHeader {}
struct Model {}
protocol PA {
typealias Ce = SCell
typealias He = SHeader
func doThis(cell : PA.Ce,header : PA.He)
}
extension PA {
func doThis(cell : PA.Ce,header : PA.He) {
print("A's implementation")
}
}
protocol PB:PA {
}
extension PB {
func doThis(cell : PA.Ce,header : PA.He) {
print("B's implementation")
}
}
class A : PA {
func doSomethingElse() {
self.doThis(cell: Cell(), header: Header())
}
}
class B : A,PB {
}
class C : B {}
let c = C()
c.doSomethingElse()
令我惊讶的是,这开始打印出来
“A的实施”
我期待它打印“B 的实现”,因为doThis(cell:header)
它作为PB
s 默认实现的一部分被覆盖。这令人惊讶地没有发生。
更有趣的是,如果我这样做:
class B: A,PB {
override func doSomethingElse() {
self.doThis(cell: Cell(), header: Header())
}
}
它开始打印出来
B的实现
为什么会这样?