Introduction
: Class는 다른 Class의 method, property 그리고 다른 특성들을 상속받을 수 있다. 이때, 상속을 하는 Class는 Subclass라 부르고 상속 당하는 클래스를 Superclass라고 부른다.

Subclass는 Superclass의 method, property, subscript에 접근할 수 있으며 overriding 해서 동작을 customizing 할 수 있다. 
또한 Subclass에서 Superclass의 property observer를 만들 수 있다. 해당 property가 stored로 선언되었는지 computed로 선언되었는지 상관없이 observer를 만들 수 있다.


Defining a Base Class
: 어떤 Class도 상속받지 않은 Class를 Baseclass 라고 한다.

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

    }

}


let someVehicle = Vehicle()


print("Vehicle: \(someVehicle.description)")

// Vehicle: traveling at 0.0 miles per hour


Subclassing
: Subclassing은 기존 Class를 상속해서 새로운 Characteristics를 더하는 것이다.

class SomeSubclass: SomeSuperclass {

    // subclass definition goes here

}

":" 뒤에 Superclass를 명시한다.

class Bicycle: Vehicle {

    var hasBasket = false

}


let bicycle = Bicycle()


bicycle.hasBasket = true

bicycle.currentSpeed = 15.0


print("Bicycle: \(bicycle.description)")

// Bicycle: traveling at 15.0 miles per hour


class Tandem: Bicycle {

    var currentNumberOfPassengers = 0

}


let tandem = Tandem()

tandem.hasBasket = true

tandem.currentNumberOfPassengers = 2

tandem.currentSpeed = 22.0

print("Tandem: \(tandem.description)")

// Tandem: traveling at 22.0 miles per hour


Overriding
: overriding은 inherit과 다르게 상속받은 instance method, type method, instance property, type property, subscript를 재정의(덮어쓰기) 하는 것이다. 
앞에 "override" 키워드를 붙여 표시한다.

Accessing Superclass Methods, Properties, and Subscripts
: Superclass의 method, property, subscript를 overriding된 각 구현부에서 접근할 수 있다.

- overriding된 Subclass의 method 구현부에서 "super" 키워드를 통해 Superclass의 override한 method를 접근할 수 있다.
- overriding된 Subclass의 property getter, setter 구현부에서 "super" 키워드를 통해 Superclass의 override한 property를 접근할 수 있다.
- overriding된 Subclass의 subscript 구현부에서 "super[someIndex]" 와 같은 문법으로 Superclass의 subscript에 접근할 수 있다.

Overriding Methods

class Train: Vehicle {

    override func makeNoise() {

        print("Choo Choo")

    }

}

let train = Train()

train.makeNoise()

// Prints "Choo Choo"

위와같이 "override" 키워드를 붙여서 해당 method를 override 했으며, 해당 instance는 override된 method를 호출한다.

Overriding Properties
: property를 overriding 하는 방법은 subclass에서 해당 property의 getter, setter를 재정의 하거나 property observer를 추가해서 custom한 동작을 구현하는 것이다.

Overriding Property Getters and Setters
property가 stored 인지 computed 인지 상관없이 Getter, Setter를 통해 overriding 할 수 있다.
Subclass 에서는 Superclass의 properties가 stored 인지, computed 인지 알지 못한다. 단지 property 이름과 type 정보만 알 뿐이다. 
override 할 때 해당 property의 이름과 type은 변경할 수 없다.
하지만  getter, setter를 제공해서 read-only property 에서 read-write property로 바꿀 수 있다.
그러나 read-write property를 read-only property로 바꿀 수 없다.

*property를 override 하려고 setter를 정의한 경우에는 getter도 함께 정의하여야 한다. getter의 동작은 override 하고 싶지 않은 경우에는 구현부에 super.someProperty를 단순히 return 하면 된다.

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

print("Car: \(car.description)")

// Car: traveling at 25.0 miles per hour in gear 3

Overriding Property Observers
property가 stored 인지 computed 인지 상관없이 Observer를 통해 overriding 할 수 있다.
다만, read-only property인 경우 overriding 할 수 없다. willset, didset method를 구현할 수 없기 때문이다.  또한 하나의 property에 setter와 property observer를 동시에 구현할 수 없다. property observer의 기능을 setter 안에서 모두 구현이 가능하기 때문이다.

class AutomaticCar: Car {

    override var currentSpeed: Double {

        didSet {

            gear = Int(currentSpeed / 10.0) + 1

        }

    }

}


let automatic = AutomaticCar()

automatic.currentSpeed = 35.0

print("AutomaticCar: \(automatic.description)")

// AutomaticCar: traveling at 35.0 miles per hour in gear 4


Preventing Overrides
: "final" 키워드를 method, property, subscript 앞에 붙여 overriding 되는것을 막을 수 있다. 
"final" 키워드는 extension 안에서의 method, property, subscript 에도 붙일 수 있다.
그리고 Class 앞에 "final" 키워드를 붙여서 해당 Class는 subclassing을 불가능하게 만들 수 있다. 


'IOS > Swift' 카테고리의 다른 글

[Swift 4.0] Initialization(2)  (0) 2017.04.01
[Swift 4.0] Initialization(1)  (0) 2017.04.01
[Swift 4.0] Subscripts  (0) 2017.04.01
[Swift 4.0] Methods  (0) 2017.04.01
[Swift 4.0] Properties  (0) 2017.04.01
Posted by 홍성곤
,