๐Ÿฆ… Swift

Inheritance

Kiwi๐Ÿ’ป 2022. 7. 5. 23:45

ํด๋ž˜์Šค๋Š” ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ์™€ ๋‹ค๋ฅธ ํŠน์ง•(characteristics)์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ ๋ถ€ํ„ฐ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด Swift์—์„œ ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํƒ€์ž…๊ณผ ๊ตฌ๋ถ„๋˜๋Š” ๊ทผ๋ณธ์ ์ธ ์š”์†Œ์ด๋‹ค. ํด๋ž˜์Šค์—์„œ๋Š” ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ์™€ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ์™€ ์ƒ๊ด€์—†์ด ์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ์— ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋ฅผ ์„ค์ •ํ•ด์„œ ๊ฐ’ ์„ค์ •์— ๋ฐ˜์‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ๋ฐ˜ ํด๋ž˜์Šค ์ •์˜ (Defining a Base Class)

๋‹ค๋ฅธ ์–ด๋–ค ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ๋„ ์ƒ์†๋ฐ›์ง€ ์•Š์€ ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค๋ผ ํ•œ๋‹ค

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
    }
}

์œ„ ์ฝ”๋“œ๋Š” ํƒˆ๊ฒƒ(Vehicle)์ด๋ผ๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ •์˜ํ•œ ๊ฒƒ์ด๋‹ค. ์ด ํƒˆ๊ฒƒ์€ ํ˜„์žฌ์†๋„(currentSpeed)๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ๊ฐ–๊ณ  ์žˆ๊ณ  description์„ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋กœ ์„ ์–ธํ–ˆ๋‹ค. makeNoise()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋„ ๊ฐ–๊ณ  ์žˆ๋‹ค. ์ด ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ํƒˆ๊ฒƒ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.

์„œ๋ธŒํด๋ž˜์‹ฑ (Subclassing)

์ƒ์†์„ ํ•˜๋ฉด ๋ถ€๋ชจ๋กœ ๋ถ€ํ„ฐ ์„ฑ๊ฒฉ์„ ์ƒ์†๋ฐ›๊ณ  ์ž๊ธฐ ์ž์‹  ๊ณ ์œ ์˜ ํŠน์„ฑ๋„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

class SomeSubclass: SomeSuperclass {
    // subclass definition goes here
}

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

Bicycle๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๊ณ ์œ  ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฌผ๋ ค๋ฐ›์€ currentSpeed, description์†์„ฑ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์„œ๋ธŒํด๋ž˜์Šค๋กœ ์ƒ์„ฑ๋œ ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์„œ๋ธŒํด๋ž˜์‹ฑ ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

Tandem๊ฐ์ฒด ์—์„œ๋Š” Tandem๊ฐ์ฒด ๊ณ ์œ  ์†์„ฑ์ธ currentNumberOfPassengers์™€ ์ž์‹ ์˜ ๋ถ€๋ชจ Bicycle์˜ ์†์„ฑ ๊ทธ๋ฆฌ๊ณ  ์กฐ๋ถ€๋ชจ์ธ Vehicle์˜ ์†์„ฑ๋„ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ค๋ฒ„๋ผ์ด๋”ฉ (Overriding)

์„œ๋ธŒํด๋ž˜์Šค์—์„œ๋Š” ๋ถ€๋ชจํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์€ ๊ฒƒ์„ ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์„ overriding ์ด๋ผ ๋ถ€๋ฅด๋Š”๋ฐ, ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ํƒ€์ž… ๋ฉ”์†Œ๋“œ, ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ, ํƒ€์ž… ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๋ชจ๋‘์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋‹ค๋ฅธ ์„ ์–ธ ์•ž์— overrideํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค. Swift์—์„œ๋Š” ์ด overrideํ‚ค์›Œ๋“œ๋ฅผ ๋ณด๋ฉด ๋ถ€๋ชจ์— ๊ทธ ์ •์˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ์˜ ์ ‘๊ทผ (Accessing Superclass Methods, Properties, and Subscripts)

superํ‚ค์›Œ๋“œ์™€ ์ ๋ฌธ๋ฒ• ํ˜น์€ ์ธ๋ฑ์Šค ๊ตฌ๋ฌธ์œผ๋กœ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. super.someMethod, super.someProperty, super[someIndex]

๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ (Overriding Methods)

์ƒ์†๋ฐ›์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์†Œ๋“œ ์„ ์–ธ ์•ž์— overrideํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ณ  ๋‚ด์šฉ์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

class Train: Vehicle {
    override func makeNoise() {
        print("Choo Choo")
    }
}

let train = Train()
train.makeNoise()
// Prints "Choo Choo" : ์ƒˆ๋กœ ์ •์˜ํ•œ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

ํ”„๋กœํผํ‹ฐ ์˜ค๋ฒ„๋ผ์ด๋“œ (Overriding Properties)

์„œ๋ธŒํด๋ž˜์Šค์—์„œ๋Š” ์ƒ์†๋ฐ›์€ ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ, ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ ๋ชจ๋‘ ์˜ค๋ฒ„๋ผ์ด๋“œ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋“œ์‹œ์—๋Š” ํ”„๋กœํผํ‹ฐ์˜ ์ด๋ฆ„๊ณผ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์•ผํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์„œ๋ธŒํด๋ž˜์Šค์—์„œ๋Š” ๋‹จ์ˆœํžˆ ์ƒ์†๋ฐ›์€ ํŠน์ • ํ˜•์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค๋Š” ์ •๋„๋งŒ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ƒ์†๋ฐ›์€ ์ฝ๊ธฐ์ „์šฉ(read only) ํ”„๋กœํผํ‹ฐ๋„ getter/setter๋ฅผ ์ •์˜ํ•ด์„œ ์ฝ๊ณ /์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ(read-write) ํ”„๋กœํผํ‹ฐ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์ œ๊ณต ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋Œ€์˜ ์ฝ๊ณ /์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๊ธฐ์ „์šฉ ํ”„๋กœํผํ‹ฐ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์€ ํ•  ์ˆ˜ ์—†๋‹ค.

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)

์ด๋ฏธ ๋ถ€๋ชจํด๋ž˜์Šค์— ์„ ์–ธ๋œ ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋„ ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

NOTEโ—๏ธ
์ƒ์ˆ˜ ํ”„๋กœํผํ‹ฐ์™€ ์ฝ๊ธฐ์ „์šฉ ํ”„๋กœํผํ‹ฐ์—๋Š” ์˜ต์ €๋ฒ„๋ฅผ ๋ถ™์ผ ์ˆ˜ ์—†๋‹ค. ์ด ํ”„๋กœํผํ‹ฐ๋Š” ์ •์˜ ๊ทธ๋Œ€๋กœ set์„ ํ•  ์ˆ˜ ์—†๋Š” ํ”„๋กœํผํ‹ฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜ ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ์— ์˜ต์ €๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  setter๋ฅผ ์ถ”๊ฐ€ํ•ด ๋‘˜์„ ๋™์‹œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋ฏธ 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

'๐Ÿฆ… Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Initialization  (0) 2022.07.13
Subscripts  (0) 2022.07.04
Methods  (0) 2022.07.04
Properties  (0) 2022.07.04
Structures and Classes  (0) 2022.06.22