Swift Inheritance
Inheritance - Bass Class, Subclassing, Overriding, Overriding Properties, Overriding Property Observers, Prevent Overrides
1. Inheritance π©βπ»
1. Defining a Base Class
λ€λ₯Έ Classes λ₯Ό μμνμ§ μμ Class λ₯Ό 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
}
}
Universal Base Class
λ₯Ό νλ λ§λ€μ΄ λͺ¨λ Classes κ° κΈ°λ³Έμ μΌλ‘ μ΄κ²μ μμνλλ‘ νλ μΌλΆ μΈμ΄μ λ¬λ¦¬ Swift λ λͺ μμ μΌλ‘ μμμ νμ§ μμ Class λbuild
ν λ μλμΌλ‘Base Class
κ° λλ€.
2. Subclassing
Subclassing
μ μ‘΄μ¬νλ λ€λ₯Έ Class λ₯Ό κΈ°λ°μΌλ‘ new Class λ₯Ό μμ±νλ νμλ₯Ό λ§νλ€.
κΈ°μ‘΄μ Class λ₯Ό Superclass
, κΈ°μ‘΄μ Class λ₯Ό μμν΄ μλ‘ μμ±λ Class λ₯Ό Subclass
λΌ νλ©°,
Subclass λ μλ‘μ΄ κ²μ μΆκ°νλ κ²μ λ¬Όλ‘ μ΄κ³ , κΈ°μ‘΄μ κ²μ μμ ν μ μλ€.
Subclassing μ :
μ μ΄μ©ν΄ μμ±νλ€.
Syntax
class SomeSubclass: SomeSuperclass {
// subclass definition goes here
}
μ Vehicle Class λ₯Ό μμν Bicycle Class λ₯Ό λ§λ€μ΄λ³΄μ.
class Bicycle: Vehicle {
var hasBasket = false
}
- Vehicle μ
Base Class
μ΄λ©΄μ, Bicycle μSuperclass
λ€. - Bicycle μ Vehicle μ
Subclass
λ‘, Vehicle μ λͺ¨λcharacteristics
λ₯Ό μμνλ€.
let bicycle = Bicycle()
bicycle.hasBasket = true
bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)") // Bicycle: traveling at 15.0 miles per hour
κ·Έλ¦¬κ³ Subclass λ λ λ€λ₯Έ Subclassing λ μ μλ€.
class Tandem: Bicycle {
var currentNumberOfPassengers = 0
}
- Bicycle μ Vehicle μ
Subclass
μ΄λ©΄μ Tandem μSuperclass
λ€. - Tandem μ Bicycle μ
Subclass
λ‘, Bicycle μ λͺ¨λcharacteristics
λ₯Ό μμνλ―λ‘ Vehicle μ λͺ¨λcharacteristics
λν μλμΌλ‘ μμνλ€.
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
2. Overriding π©βπ»
1. Overriding
Subclass λ Superclass μ Instance Methods, Type Methods, Instance Properties,
Type Properties, Subscripts λ₯Ό λ€μ ꡬνν μ μλ€. μ΄κ²μ Overriding
μ΄λΌ νλ€.
Overriding μ μν΄μ μμ override
modifier λ₯Ό λΆμ¬μ€λ€.
μ΄λ κ² νλ κ²μ μ¬μ μλ₯Ό λͺ
νν νκ³ , μ€μλ‘ μ¬μ μνλ κ²μ λ°©μ§νκΈ° μν κ²μΌλ‘, override
modifier μμ΄ μ¬μ μνλ©΄
Swift λ μ΄λ₯Ό νμΈνκ³ compile error
λ₯Ό λ°μμν¨λ€.
Overriding
κ°λ₯νcharacteristics
λmutable
ν κ²μΌλ‘ μ νλλ€. μλ₯Ό λ€μ΄let
ν€μλλ‘ μ μΈλ κ²½μ°immutable
μ΄κΈ° λλ¬Έμ Overriding ν μ μλ€.
2. Accessing Superclass Methods, Properties, and Subscripts
λλ‘λ Superclass μ μ κ·Όνλ κ²μ΄ μ μ©ν λκ° μμΌλ©°, super
ν€μλλ₯Ό μ΄μ©ν΄ μ κ·Όν μ μλ€.
λ€μμ Overriding
λ characteristics
μ μΌμ΄μ€λ³ super
μ κ·Ό μμλ€.
- someMethod() : super.someMethod() λ₯Ό νΈμΆνλ€.
- someProperty : getter, setter λ₯Ό μ΄μ©ν΄ super.Property μ μ κ·Όνλ€.
- someIndex: super[someIndex] λ‘ μ κ·Όνλ€.
class TimesTable {
let multiplier: Int
subscript(index: Int) -> Int { multiplier * index }
func printMultiplier() {
print(multiplier)
}
init(multiplier: Int) {
self.multiplier = multiplier
}
}
class ArithmeticSequenceTable: TimesTable {
var superMultiplier: Int { super.multiplier }
override func printMultiplier() {
super.printMultiplier()
}
override subscript(index: Int) -> Int { super[index] + 1 }
}
super
λ₯Ό μ΄μ©ν μ κ·Όμ Subclass λ΄μμ μ κ·Όνλ κ²μ΄λ€. Subclass μ Instance λ₯Ό ν΅ν΄ μ κ·Όνλ κ²μ΄ μλλ€.
3. Overriding Methods
class Train: Vehicle {
override func makeNoise() {
print("μΉμΉνν")
}
}
let train = Train()
train.makeNoise() // μΉμΉνν
4. Overriding Properties
1 ) Overriding Property Getters and Setters
class Car: Vehicle {
var gear = 1
override var description: String {
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
2 ) Overriding Property Observers
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
3 ) Overriding Stored Properties
Stored Properties
λ₯Ό Overriding
νλ κ²μ λ€λ₯Έ Properties μλ μ‘°κΈ λ€λ₯΄κΈ° λλ¬Έμ λ³λ ν¬μ€ν
Overriding Stored Properties μΌλ‘ μμ±νλ€.
3. Preventing Overrides π©βπ»
Overriding
μ λ§κΈ° μν΄ final
modifier λ₯Ό μΆκ°ν μ μλ€. λ§μ½ Subclass μμ μ¬μ μ ν κ²½μ°
Swift λ μ΄λ₯Ό νμΈνκ³ compile error
λ₯Ό λ°μμν¨λ€.
class AutomaticCar: Car {
override final var currentSpeed: Double {
didSet {
gear = Int(currentSpeed / 10.0) + 1
}
}
}
class ElectricMotorCar: AutomaticCar {
override var currentSpeed: Double { // error: Property overrides a 'final' property
}
}
AutomaticCar μ currentSpeed λ₯Ό Overriding νλ©΄μ final
modifier λ₯Ό λΆμ¬μ£ΌμκΈ° λλ¬Έμ
AutomaticCar λ₯Ό μμν ElectricMotorCar λ μ΄κ²μ μ¬μ μ ν μ μλ€.
Properties, Methods, Subscripts κ° μλ Classes μ μμ
final
modifier λ₯Ό μμ±ν κ²½μ°, μ΄ Class λ₯ΌSubclassing
νλ €λ λͺ¨λ μλλ compile-time error κ° λ°μνλ€.
Reference
- βInheritance.β The Swift Programming Language Swift 5.7. accessed Nov. 29, 2022, Swift Docs Chapter 12 - Inheritance.