1
我有以下情況,真正的代碼更多,但這提供了一個很好的情況。 (如果你想知道使用的,這是有一個動態加載UICollectionView):Swift:子類返回父類屬性
protocol P {
static var name: String {get}
}
extension P {
static var name: String {
return String(Self)
}
}
protocol P2 {
getType() -> P.Type
getName() -> String
}
class A: P {
}
class B: A {
}
class C1: P2 {
func getType() -> P.Type {
return A.self
}
func getName() -> String {
return A.name
}
}
class C2: P2 {
func getType() -> P.Type {
return B.self
}
func getName() -> String {
return B.name
}
}
現在發生的事情是這樣的:
C1.getType().name //"A" as expected
C1.getName() //"A" as expected
C2.getType().name //"A" why?
C2.getName() //"B" as expected
我沒有任何線索,爲什麼C2.getType.name
回報「一個」。我在調試器中檢查和正確執行了該功能,通過C2返回類型爲B.
我使用的XCode 7.3與2.2雨燕
好吧,我只是在子類中實現所需的方法,謝謝! – whtman