self

代表当前对象

Self

代表当前类型

Self一般作为返回值类型,限定返回值和方法调用者必须是同一类型(也可以作为参数类型)

protocol Runnable {
    func test() -> Self
}

class Person: Runnable {
    var age = 10
    static var count = 0
    
    required init() {
        print(self.age)
        print(Self.count)
    }
    
    func test() -> Self {
        type(of: self).init()
    }
    
}

let p = Person()
print(p.test())

输出结果

10
0
10
0