Inheritance by Inheritance

Learning new knowledge, always inadvertently to forget, as the saying goes good memory is not as bad as writing, I would like to record this, convenient to use when looking for the future, at the same time, if you can give a friend in need of inspiration, will also be a great encouragement to me! If there is not enough, please give advice, thank you!!

1. A simple explanation of inheritance

  • Value types (enumerations, structs) do not support inheritance; only classes do

  • Classes that have no parent are called base classes

    • Swift does not stipulate that any class must eventually inherit from a base class, as OC and Java do

    Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon

  • Subclasses can override subscripts, methods, and attributes of their parent classes. Overriding must include the override keyword

2. Rewrite instance methods and subscripts

class Animal {
    func speak() {
        print("Animal speak")
    }
    subscript(index: Int) -> Int {
        index
    }
}

class Cat : Animal {
    override func speak() {
        super.speak()
        print("Cat speak")
    }
    override subscript(index: Int) -> Int {
        super[index] + 1
    }
}

var anima: Animal = Animal()
// Animal speak
anima.speak()
// anima[6] = 6
print("anima[6] = \(anima[6])")

anima = Cat()
// Animal speak
// Cat speak
anima.speak()
// anima[6] = 7
print("anima[6] = \(anima[6])")
Copy the code

3. Rewrite type methods and subscripts

  • The method and subscript modified staticDon’tAllows to be overridden by subclasses
  • Methods and subscripts decorated by class are allowed to be overridden by subclasses
class Animal {
    class func speak() {
        print("Animal speak")
    }
    class subscript(index: Int) -> Int {
        index
    }
}

class Cat : Animal {
    override class func speak() {
        super.speak()
        print("Cat speak")
    }
    override class subscript(index: Int) -> Int {
        super[index] + 1
    }
}

// Animal speak
Animal.speak()
// anima[6] = 6
print("anima[6] = \(Animal[6])")

// Animal speak
// Cat speak
Cat.speak()
// anima[6] = 7
print("anima[6] = \(Cat[6])")
Copy the code

4. Override properties

  • Subclasses can override attributes of their parent class (storage, computation) as computed attributes
  • A subclassCan not beRewrite the parent class property toStorage properties
  • You can override only the var attribute, not the let attribute
  • When overridden, the attribute name and type must be the same
  • The overridden property of a subclass cannot be less than the property of its parent class
    • If the parent property is read-only, the property overridden by the subclass can be either read-only or read-write
    • If the parent property is read and write, the property overridden by the subclass must also be read and write
Var diameter: Int = 0 var diameter: Int { set { print("Circle setDiameter") radius = newValue / 2 } get { print("Circle getDiameter") return radius * 2 } } } var circle: Circle circle = Circle() circle.radius = 6 // Circle getDiameter // 12 print(circle.diameter) //Circle setDiameter //10 Circle. Diameter = 20 print(circle. Radius) {override var radius: Int { set { print("SubCircle setRadius") super.radius = newValue > 0 ? newValue : 0 } get { print("SubCircle getRadius") return super.radius } } override var diameter: Int { set { print("SubCircle setDiameter") super.diameter = newValue / 2 > 0 ? newValue : 0 } get { print("SubCircle getDiameter") return super.diameter } } } circle = SubCircle() //SubCircle setRadius circle.radius = 6 //SubCircle getDiameter //Circle getDiameter //SubCircle getRadius //12 print(circle.diameter) //SubCircle setDiameter //Circle setDiameter //SubCircle setRadius circle.diameter = 20 //SubCircle getRadius //10 Print (circles.radius) // The above comment content is the print resultCopy the code

5. Attribute viewer

  • Attribute observers can be added to subclasses for superclass attributes (except read-only computed attributes and let attributes)
class Circle {
    var radius: Int = 1
}

class SubCircle : Circle {

    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var circle = SubCircle()

//SubCircle willSetRadius 10
//SubCircle didSetRadius 1 10
circle.radius = 10
Copy the code
  • Add attribute observer and Override to the parent class’s calculated property
class Circle {
    var radius: Int  {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20
        }
    }
}

class SubCircle : Circle {

    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var circle = SubCircle()

//Circle getRadius
//SubCircle willSetRadius 10
//Circle setRadius 10
//Circle getRadius
//SubCircle didSetRadius 20 20
circle.radius = 10
Copy the code

6. final

  • Methods, subscripts, properties, and disallows that are modified by final are overridden
  • Classes modified by final are prohibited from being inherited