Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Learning Swift Part 11 - Inheritance

| Comments

===== Full Notes =====

Inheritance

  • A base class, Vehicle
1
2
3
4
5
6
7
8
9
    class Vehicle {
        var currentSpeed = 0.0
        var description: String {
                return "traveling at \(currentSpeed) miles per hour"
        }
        func makeNoise() {
                // do nothing - an arbitrary vehicle doesn't necessarily make a noise
        }
    }
  • Subclass, Bicycle and Tandem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    class Bicycle: Vehicle {
        var hasBasket = false
    }

    class Tandem: Bicycle {
        var currentNumberOfPassengers = 0
    }

    let tandem = Tandem()
    tandem.hasBasket = true
    tandem.currentNumberOfPassengers = 2
    tandem.currentSpeed = 22.0
    println("Tandem: \(tandem.description)")
    // Tandem: traveling at 22.0 miles per hour
  • Overriding

    • A subclass can provide its own custom implementation of an instance method, class method, instance property, class property, or subscript that it would otherwise inherit from a superclass. This is known as overriding.
    • To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the override keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Overriding by accident can cause unexpected behavior, and any overrides without the override keyword are diagnosed as an error when your code is compiled.
    • The override keyword also prompts the Swift compiler to check that your overriding class’s superclass (or one of its parents) has a declaration that matches the one you provided for the override. This check ensures that your overriding definition is correct.
  • Accessing Superclass Methods, Properties, and Subscripts

    • An overridden method named someMethod can call the superclass version of someMethod by calling super.someMethod() within the overriding method implementation.
    • An overridden property called someProperty can access the superclass version of someProperty as super.someProperty within the overriding getter or setter implementation.
    • An overridden subscript for someIndex can access the superclass version of the same subscript as super[someIndex] from within the overriding subscript implementation.
  • Overriding Methods

1
2
3
4
5
6
7
8
9
    class Train: Vehicle {
        override func makeNoise() {
            println("Choo Choo")
        }
    }

    let train = Train()
    train.makeNoise()
    // prints "Choo Choo"
  • Overriding Properties

    • You can override an inherited instance or class property to provide your own custom getter and setter for that property, or to add property observers to enable the overriding property to observe when the underlying property value changes.
  • Overriding Property Getters and Setters

    • You can present an inherited read-only property as a read-write property by providing both a getter and a setter in your subclass property override. You cannot, however, present an inherited read-write property as a read-only property.
    • If you provide a setter as part of a property override, you must also provide a getter for that override. If you don’t want to modify the inherited property’s value within the overriding getter, you can simply pass through the inherited value by returning super.someProperty from the getter, where someProperty is the name of the property you are overriding.
1
2
3
4
5
6
7
8
9
10
11
12
    class Car: Vehicle {
        var gear = 1
        override var description: String {
            return super.description + " in gear \(gear)"
        }
    }

    let car = Car()
    car.currentSpeed = 25.0
    car.gear = 3
    println("Car: \(car.description)")
    // Car: traveling at 25.0 miles per hour in gear 3
  • Overriding Property Observers
1
2
3
4
5
6
7
8
9
10
11
12
    class AutomaticCar: Car {
        override var currentSpeed: Double {
            didSet {
                gear = Int(currentSpeed / 10.0) + 1
            }
        }
    }

    let automatic = AutomaticCar()
    automatic.currentSpeed = 35.0
    println("AutomaticCar: \(automatic.description)")
    // AutomaticCar: traveling at 35.0 miles per hour in gear 4
  • Preventing Overrides
    • You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript’s introducer keyword (such as final var, final func, final class func, and final subscript).
    • Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
    • You can mark an entire class as final by writing the final modifier before the class keyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.

Comments