ただのメモです。
Swift 4だか5だか。
定義
protocol P {
static func staticFunc() -> String
}
extension P {
static func staticFunc() -> String {return "default"}
}
class A: P {
static func staticFunc() -> String {return String(describing: self)}
}
class B: P {}
class C: A {}
class D {}
//class E: A {
// static override func staticFunc() -> String {return "hogehoge"}
//}
//error: cannot override static method
実行
[A.self, B.self, C.self, D.self].forEach {
if let type = $0 as? P.Type {
print("\(type): \(type.staticFunc())")
}
}
//A: A
//B: default
//C: C
-
D
はP
に準拠していないので呼び出されない。 - overrideは出来ない。