兩階段初始化

Two-Phase Initialization

Implicit super.init()

If a subclass initializer:

  • performs no customization in phase 2 of the initialization process

  • the superclass has a zero-argument designated initializer

you can omit a call to super.init() after assigning values to all of the subclass’s stored properties

class Cat : Animal {

  override init() {
    print("Cat created")
    // super.init() implicitly called here. ⭐
  }

  override func makeNoise() {
    print("Mews")
  }
  
}

🔗 原始碼:repl.it

Last updated