有两个主要区别。
1. 价值self
静态方法内部
您在方法中可以使用调用静态方法的元类型,如下所示self
(它只是作为隐式参数传递)。因此如果你打电话doIt()
on type(of: self) https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/postfix-self-expression, self
将是dynamic实例的元类型。如果你打电话Foo
, self
将Foo.self
.
class Foo {
static func doIt() {
print("hey I'm of type \(self)")
}
func callDoItOnDynamicType() {
type(of: self).doIt() // call on the dynamic metatype of the instance.
}
func classDoItOnFoo() {
Foo.doIt() // call on the metatype Foo.self.
}
}
class Bar : Foo {}
let f: Foo = Bar()
f.callDoItOnDynamicType() // hey I'm of type Bar
f.classDoItOnFoo() // hey I'm of type Foo
这个差异可以是really对于工厂方法很重要,因为它决定了您创建的实例的类型。
class Foo {
required init() {}
static func create() -> Self {
return self.init()
}
func createDynamic() -> Foo {
return type(of: self).create()
}
func createFoo() -> Foo {
return Foo.create()
}
}
class Bar : Foo {}
let f: Foo = Bar()
print(f.createDynamic()) // Bar
print(f.createFoo()) // Foo
2. 静态方法的调度
(马丁已经报道过 https://stackoverflow.com/a/42260517/2976878这个,但我想为了完成而添加它。)
For class
对于在子类中重写的方法,您调用该方法的元类型的值决定了要调用哪个实现。
如果在编译时已知的元类型上调用(例如Foo.doIt()
),Swift 能够静态地调度调用。但是,如果您在运行时才知道的元类型上调用该方法(例如type(of: self)
),方法调用将是动态地分派到元类型值的正确实现。
class Foo {
class func doIt() {
print("Foo's doIt")
}
func callDoItOnDynamicType() {
type(of: self).doIt() // the call to doIt() will be dynamically dispatched.
}
func classDoItOnFoo() {
Foo.doIt() // will be statically dispatched.
}
}
class Bar : Foo {
override class func doIt() {
print("Bar's doIt")
}
}
let f: Foo = Bar()
f.callDoItOnDynamicType() // Bar's doIt
f.classDoItOnFoo() // Foo's doIt