[Swift🦩] #13 상속

λ˜μƒΒ·2022λ…„ 3μ›” 30일
0

iOS

λͺ©λ‘ 보기
26/42
post-thumbnail
  • 클래슀 λŠ” λ©”μ†Œλ“œ, ν”„λ‘œνΌν‹°, νŠΉμ§• 을 λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œ μƒμ†ν•΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
  • ꡬ쑰체와 클래슀의 κ°€μž₯ 큰 차이점.
  • μ €μž₯ ν”„λ‘œνΌν‹°μ™€ 계산 ν”„λ‘œνΌν‹°μ— 관계없이, 상속받은 ν”„λ‘œνΌν‹°μ— ν”„λ‘œνΌν‹° μ˜΅μ €λ²„λ₯Ό μ„€μ •ν•  수 μžˆλ‹€.

1. Base Class μ •μ˜

  • λ‹€λ₯Έ μ–΄λ–€ ν΄λž˜μŠ€λ„ 상속받지 μ•Šμ€ 클래슀.

μ˜ˆμ‹œ) currentSpeed 와 makeNoise λ₯Ό 가지고 μžˆλŠ” Vehicle 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
    }
}

print("Vehicle: \(someVehicle.description)")
// Vehicle: traveling at 0.0 miles per hour

2. Subclassing == 상속

  • λΆ€λͺ¨λ‘œλΆ€ν„° 성격을 상속받고, μžμ‹ μ˜ 고유 νŠΉμ„±μ„ μΆ”κ°€ν•  μˆ˜λ„ μžˆλ‹€.
// Vehicle 의 ν”„λ‘œνΌν‹°, λ©”μ„œλ“œ, νŠΉμ§•μ„ μ‚¬μš©ν•˜λ©΄μ„œ,
// Bicycle 만의 ν”„λ‘œνΌν‹°μΈ hasBasket 도 μΆ”κ°€.
class Bicycle: Vehicle {
    var hasBasket = false
}

bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)")
// Bicycle: traveling at 15.0 miles per hour


// ν•œλ²ˆ subclassing 된 것을 λ‹€μ‹œ subclassing κ°€λŠ₯.
class Tandem: Bicycle {
    var currentNumberOfPassengers = 0
}

3. Overriding ⭐️

  • μ„œλΈŒν΄λž˜μŠ€μ—μ„œ λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ 상속받은 것을 μž¬μ •μ˜ν•˜λŠ” 것.
  • μΈμŠ€ν„΄μŠ€/νƒ€μž… ν”„λ‘œνΌν‹°, λ©”μ„œλ“œ, μ„œλΈŒμŠ€ν¬λ¦½νŠΈμ— λͺ¨λ‘ κ°€λŠ₯ν•˜λ‹€.
  • override ν‚€μ›Œλ“œλ₯Ό λΆ™μ—¬μ„œ λΆ€λͺ¨μ˜ 것을 μž¬μ •μ˜ ν•˜λŠ” κ²ƒμž„μ„ μ•Œλ¦°λ‹€.

4. λΆ€λͺ¨ 클래슀의 것에 μ ‘κ·Ό.

  • μžμ‹μ—μ„œ λΆ€λͺ¨μ˜ λ©”μ†Œλ“œ, ν”„λ‘œνΌν‹°, μ„œλΈŒμŠ€ν¬λ¦½νŠΈμ— μ ‘κ·Όν•  수 μžˆλ‹€.
  • super ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
  • super.someMethod, super.someProperty, super[someIndex]

overriding method

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

let train = Train()
train.makeNoise()
// Prints "Choo Choo" : μƒˆλ‘œ μ •μ˜ν•œ λ‚΄μš©μ΄ 좜λ ₯λ©λ‹ˆλ‹€.

properties

  • μ €μž₯ ν”„λ‘œνΌν‹°, 계산 ν”„λ‘œνΌν‹° λͺ¨λ‘ μ˜€λ²„λΌμ΄λ“œκ°€ κ°€λŠ₯ν•˜λ‹€.
  • νƒ€μž…, 이름을 λͺ¨λ‘ λͺ…μ‹œν•΄μ•Ό ν•œλ‹€.
  • λΆ€λͺ¨μ˜ read-only property λ₯Ό read-write property 둜 μž¬μ •μ˜ν•  수 μžˆλ‹€. (getter/setter λͺ¨λ‘ μ •μ˜ν•΄μ„œ)
  • λ°˜λŒ€λ‘œ λΆ€λͺ¨μ˜ read-write property λ₯Ό read-only 둜 μž¬μ •μ˜ ν•˜λŠ” 것은 λΆˆκ°€λŠ₯ν•˜λ‹€.
  • setter λ₯Ό override ν• κ±°λ©΄ getter 도 λ°˜λ“œμ‹œ μ œκ³΅ν•΄μ•Ό 함.
  • πŸ“• μ €μž₯ ν”„λ‘œνΌν‹°λ₯Ό μ €μž₯ ν”„λ‘œνΌν‹°λ‘œ μž¬μ •μ˜ ν•˜λŠ” 것은 λΆˆκ°€λŠ₯ν•˜λ‹€.
  • πŸ“• μ—°μ‚° ν”„λ‘œνΌν‹°, ν”„λ‘œνΌν‹° μ˜΅μ €λ²„ λ₯Ό μΆ”κ°€ν•  수 있고, getter 에 λ”±νžˆ ν•„μš” κΈ°λŠ₯이 ν•„μš” μ—†λ‹€λ©΄, super.someProperty 처럼 λΆ€λͺ¨μ— μ ‘κ·Όν•΄μ„œ 값을 λ°›μ•„μ„œ λ°˜ν™˜ν•˜μž.
class Car: Vehicle {
    var gear = 1
    override var description: String {
        return super.description + " in gear \(gear)"
    }
}

Property Observers

  • λΆ€λͺ¨ ν΄λž˜μŠ€μ— μ„ μ–Έλœ ν”„λ‘œνΌν‹° μ˜΅μ €λ²„λ₯Ό μž¬μ •μ˜ ν•˜λŠ” 것도 κ°€λŠ₯.
  • ν•˜μ§€λ§Œ, let μ΄λ‚˜ read-only μ—λŠ” ν”„λ‘œνΌν‹° μ˜΅μ €λ²„λ₯Ό 뢙일 수 μ—†λ‹€.
    set ν•  수 μ—†κΈ° λ•Œλ¬Έ
  • 같은 ν”„λ‘œνΌν‹°μ— observer 와 setter λ₯Ό λ™μ‹œμ— μΆ”κ°€ν•΄μ„œ μ‚¬μš©ν•˜λŠ” 것도 ν•  수 μ—†λ‹€.
    setter λŠ” observer와 같은 λ™μž‘μ„ ν•˜κΈ° λ•Œλ¬Έ
  • πŸ“• λ‘˜ λ‹€ λ™μž‘ν•˜λ©΄ μ’‹κ² λ‹€? -> λΆ€λͺ¨λ₯Ό 계산 ν”„λ‘œνΌν‹°λ‘œ λ§Œλ“€μ–΄μ„œ setter λ„£κ³  override ν•˜λŠ” μͺ½μ—μ„œ observer κ΅¬ν˜„ν•˜μž.
class AutomaticCar: Car {
    override var currentSpeed: Double {
        didSet {
            gear = Int(currentSpeed / 10.0) + 1
        }
    }
}

μ˜€λ²„λΌμ΄λ“œ 방지

  • μžμ‹ ν΄λž˜μŠ€μ—μ„œ νŠΉμ • λ©”μ†Œλ“œ, ν”„λ‘œνΌν‹°, μ„œλΈŒμŠ€ν¬λ¦½νŠΈκ°€ μ˜€λ²„λΌμ΄λ“œ λ˜μ§€ μ•Šκ²Œ ν•˜λ €λ©΄
  • final ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ˜€λ²„λΌμ΄λ“œλ₯Ό λ°©μ§€ν•œλ‹€.

-> BaseVC 같은 것 말고, λ§ˆμ§€λ§‰μœΌλ‘œ μ„ μ–Έν•˜λŠ” VCμ—λŠ” final 을 μ¨μ„œ μ΅œμ ν™”λ₯Ό ν•΄μ£Όμž.

profile
0λ…„μ°¨ iOS κ°œλ°œμžμž…λ‹ˆλ‹€.

0개의 λŒ“κΈ€