ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] Objected Oriented Programming in Swift (raywenderlich ๋ฒˆ์—ญ)
    Swift 2022. 1. 13. 19:57

    ์‚ฌ๋ฌผ์„ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ธ ๊ฐœ๋…์„ ๋ณด๋‹ค ๊ตฌ์ฒด์ ์ธ ์œ ํ˜•์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฎค์ง€์ปฌ ๋ฐด๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ํ‘œ๋ฅผ ์•…๊ธฐ์˜ ๊ฐ€๊ณ„๋„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด, ์ž์‹๊ณผ ๋ถ€๋ชจ ์‚ฌ์ด์˜ ๊ด€๊ณ„๋Š” is-a์ด๋‹ค. ์ฆ‰, Guitar is-a Instrument๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

    แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-01-13 แ„‹แ…ฉแ„’แ…ฎ 1 48 55 ใ…ค ใ…ค

    Properties

    // 1
    class Instrument {
      // 2
      let brand: String
      // 3
      init(brand: String) {
        //4 
        self.brand = brand
      }
    }
    1. class ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ Instrument๋ผ๋Š” base class๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ, ์ด๊ฒŒ ๋ฐ”๋กœ instruments ๊ณ„์ธต์˜ ๋ฃจํŠธ ํด๋ž˜์Šค์ด๋‹ค.
    2. instrument์˜ stored properties (data)๋ฅผ ์„ ์–ธํ–ˆ๋Š”๋ฐ, ์ด๋Š” ๋ชจ๋“  instruments๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
    3. init ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํด๋ž˜์Šค์˜ initializer๋ฅผ ์ƒ์„ฑํ–ˆ๋Š”๋ฐ, ์ด๋Š” ๋ชจ๋“  stored properties๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์ƒˆ๋กœ์šด instruments๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค.
    4. instruments์˜ stored property์ธ brand๋ฅผ parameter๋กœ ์ „๋‹ฌ๋œ brand๋กœ ์„ค์ •ํ•œ๋‹ค. ์ด ๋•Œ, property์™€ parameter์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฏ€๋กœ, self ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
      ใ…ค
      ใ…ค

    Methods

    ์•…๊ธฐ์˜ ์ข…๋ฅ˜์— ์ƒ๊ด€ ์—†์ด ์กฐ์œจํ•˜๊ฑฐ๋‚˜ ์—ฐ์ฃผ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, Instrument ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    func tune() -> String {
      fatalError("Implement this method for \(brand)")
    }
    • tune() method๋Š” ํ˜ธ์ถœํ•˜๋ฉด ๋Ÿฐํƒ€์ž„์— ํฌ๋ž˜์‹œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” placeholder ํ•จ์ˆ˜์ด๋‹ค.
    • ์ด์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ์ง์ ‘์ ์ธ ์‚ฌ์šฉ์„ ์˜๋„ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ƒ์  (abstract)์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋Œ€์‹ ์—, ๋ฉ”์†Œ๋“œ๋ฅผ overrideํ•ด์„œ ์ ์ ˆํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋Š” subclass๋ฅผ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.
    • class ๋‚ด๋ถ€์— ์ •์˜๋œ ํ•จ์ˆ˜๋“ค์€ Instrument์˜ brand์™€ ๊ฐ™์€ property์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ํ•œ๋‹ค.
    • class์—์„œ property๋“ค๊ณผ ๊ด€๋ จ ์ž‘์—…์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋ณต์žก์„ฑ์„ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ด๋‹ค. ์ด๋ฅผ ์‹ฌ์ง€์–ด encapsulation๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ, Class ํƒ€์ž…์€ ๋ฐ์ดํ„ฐ (ex. stored properties)์™€ ๋™์ž‘ (ex. methods)์„ ์บก์Аํ™” (encapsulate) ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.
      ใ…ค
      ใ…ค

    ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    class Music {
      let notes: [String]
    
      init(notes: [String]) {
        self.notes = notes
      }
    
      func prepared() -> String {
        return notes.joined(separator: " ")
      }
    }
    • ์ด Music class๋Š” ๋ฐฐ์—ด notes๋ฅผ ์บก์Аํ™”ํ•˜๊ณ  prepared() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
      ใ…ค
      ใ…ค

    Instrument ํด๋ž˜์Šค์˜ tune() ๋ฉ”์†Œ๋“œ ๋’ค์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    func play(_ music: Music) -> String {
      return music.prepared()
    }
    • play(_:) ๋ฉ”์†Œ๋“œ๋Š” ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด ์•„๋‹Œ Music ํƒ€์ž…์„ ์ „๋‹ฌํ•œ ์ด์œ ๋Š”, Music ํƒ€์ž…์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ›„์— ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ™•์žฅ์„ ์œ„ํ•œ ์žฅ์†Œ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
      ใ…ค
      ใ…ค

    Instrument ํด๋ž˜์Šค์˜ play(_:) ๋ฐ”๋กœ ๋’ค์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    func perform(_ music: Music) {
      print(tune())
      print(play(music))
    }
    • perform( _ :) ๋ฉ”์†Œ๋“œ๋Š” tune() ๋ฉ”์†Œ๋“œ๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•˜๊ณ , ์ด์–ด์„œ play(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค.

    ใ…ค
    ใ…ค

    Inheritance

    ์•…๊ธฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    // 1
    class Piano: Instrument {
      let hasPedals: Bool
      // 2
      static let whiteKeys = 52
      static let blackKeys = 36
    
      // 3
      init(brand: String, hasPedals: Bool = false) {
        self.hasPedals = hasPedals
        // 4
        super.init(brand: brand)
      }
    
      // 5
      override func tune() -> String {
        return "Piano standard tuning for \(brand)."
      }
    
      override func play(_ music: Music) -> String {
        // 6 
        let preparedNotes = super.play(music)
        return "Piano playing \(preparedNotes)"
      }
    }
    1. Piano ํด๋ž˜์Šค๋ฅผ Instrument ํด๋ž˜์Šค์˜ subclass๋กœ ์ƒ์„ฑํ–ˆ๋‹ค. Instrument ํด๋ž˜์Šค๋Š” Piano ํด๋ž˜์Šค์˜ parent class๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋ชจ๋“  stored properties์™€ methods๊ฐ€ ์ž๋™์œผ๋กœ child class์ธ Piano ํด๋ž˜์Šค๋กœ ์ƒ์† (inherited) ๋˜๊ณ , Piano ํด๋ž˜์Šค ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
    2. ๋ชจ๋“  piano๋“ค์€ ๋ธŒ๋žœ๋“œ์— ๊ด€๊ณ„์—†์ด ์ •ํ™•ํžˆ ๊ฐ™์€ ์ˆซ์ž์˜ ํฐ์ƒ‰ ํ‚ค์™€ ๊ฒ€์€์ƒ‰ ํ‚ค๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด ์†์„ฑ๊ณผ ๊ด€๋ จ๋œ ๊ฐ’๋“ค์€ ๋ณ€ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ static ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค.
    3. ์ƒ์„ฑ์ž๋Š” hasPedals ํŒŒ๋ผ๋ฏธํ„ฐ ๊ธฐ๋ณธ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์›ํ•œ๋‹ค๋ฉด ์ด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
    4. child class์˜ hasPedals ์†์„ฑ์„ ์„ค์ •ํ•œ ํ›„์— super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ parent class์˜ ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค. super class ์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ์ƒ์†๋œ ์†์„ฑ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” brand๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
    5. override ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ tune ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์žฌ์ •์˜ํ–ˆ๋‹ค. ์ด๋Š” parent class์—์„œ์˜ ๊ตฌํ˜„์ฒ˜๋Ÿผ fatalError()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์ฆ‰, ๊ธฐ๋Šฅ์ ์ธ ๋ฉด์œผ๋กœ ๋งํ•˜์ž๋ฉด ํ”ผ์•„๋…ธ์— ํŠนํ™”๋œ ํŠœ๋‹์„ ํ•˜๋Š” ์—ญํ• ๋กœ ์žฌ์ •์˜๋˜์—ˆ๋‹ค.
    6. ์ƒ์†๋œ play(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๊ณ , ์ด ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์—์„œ super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ parent class์˜ play ๋ฉ”์†Œ๋“œ๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  preparedNotes๋ฅผ ์–ป์€ ํ›„ ์ด๋ฅผ ์—ฐ์ฃผํ•œ๋‹ค๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.
      ใ…ค
      ใ…ค
    ใ…ค ใ…ค

    Method Overloading

    Piano class์˜ ์žฌ์ •์˜๋œ play(_:) ๋ฉ”์†Œ๋“œ ๋’ค์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    func play(_ music: Music, usingPedals: Bool) -> String {
      let preparedNotes = super.play(music)
      if hasPedals && usingPedals {
        return "Play piano notes \(preparedNotes) with pedals."
      }
      else {
        return "Play piano notes \(preparedNotes) without pedals."
      }
    }
    • play(_:) ๋ฉ”์†Œ๋“œ๋Š” usingPedals๊ฐ€ ์ฐธ์ด๊ณ  ์‹ค์ œ๋กœ ํ”ผ์•„๋…ธ๊ฐ€ ํŽ˜๋‹ฌ์ด ์žˆ์„ ๋•Œ, ํŽ˜๋‹ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜ค๋ฒ„๋กœ๋“œํ•œ๋‹ค.
    • ๋‹ค๋ฅธ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— override ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. Swift๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก์„ ํ†ตํ•ด ์‚ฌ์šฉํ•  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
    • ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์ด ๊ฐ™์•„์„œ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์กฐ์‹ฌํžˆ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
      ใ…ค
      ใ…ค

    Piano ํด๋ž˜์Šค์˜ play(_:)๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

    override func play(_ music: Music) -> String {
      return play(music, usingPedals: hasPedals)
    }

    ใ…ค
    ใ…ค

    Instances

    // 1
    let piano = Piano(brand: "Yamaha", hasPedals: true)
    piano.tune()
    // 2
    let music = Music(notes: ["C", "G", "F"])
    // 3
    piano.play(music)
    // 4
    Piano.whiteKeys
    Piano.blackKeys
    1. piano ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    2. music ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    3. ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ•ญ์ƒ ํŽ˜๋‹ฌ์„ ์‚ฌ์šฉํ•˜๋Š” Piano ํด๋ž˜์Šค์˜ play(_:)๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    4. ํ‚ค์˜ ๊ฐฏ์ˆ˜๋Š” Piano ํด๋ž˜์Šค์— ์žˆ๋Š” static ์ƒ์ˆ˜์ด๋ฏ€๋กœ, ํ‚ค์˜ ๊ฐฏ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ์— ํŠน์ • ์ธ์Šคํ„ด์Šค๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๊ทธ์ € ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.
      ใ…ค
      ใ…ค

    Intermediate Abstract Base Classes

    class Guitar: Instrument {
      let stringGauge: String
    
      init(brand: String, stringGauge: String = "medium") {
        self.stringGauge = stringGauge
        super.init(brand: brand)
      }
    }
    • Instrument์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Guitar๋Š” ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ tune()๊ณผ play(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์•ผํ•˜๋Š” ์ถ”์ƒ ํƒ€์ž…์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” Intermediate Abstract Base Class๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.
      ใ…ค
      ใ…ค

    Concrete Guitars

    class AcousticGuitar: Guitar {
      static let numberOfStrings = 6
      static let fretCount = 20
    
      override func tune() -> String {
        return "Tune \(brand) acoustic with E A D G B E"
      }
    
      override func play(_ music: Music) -> String {
        let preparedNotes = super.play(music)
        return "Play folk tune on frets \(preparedNotes)."
      }
    }
    • ๋ชจ๋“  ์–ด์ฟ ์Šคํ‹ฑ ๊ธฐํƒ€๋“ค์€ 6๊ฐœ์˜ ์ค„๊ณผ 20๊ฐœ์˜ ํ”„๋ ›์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— numberOfStrings, fretCount๋Š” static ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.
    • ์ด ํด๋ž˜์Šค์—์„œ๋Š” ์ƒˆ๋กœ์šด stored property๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, Guitar๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋ฏ€๋กœ ๋”ฐ๋กœ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†๋‹ค.
      ใ…ค
      ใ…ค

    Private

    // 1
    class Amplifier {
      // 2
      private var _volume: Int
      // 3
      private(set) var isOn: Bool
    
      init() {
        isOn = false
        _volume = 0
      }
    
      // 4
      func plugIn() {
        isOn = true
      }
    
      func unplug() {
        isOn = false
      }
    
      // 5
      var volume: Int {
        // 6
        get {
          return isOn ? _volume : 0
        }
        // 7
        set {
          _volume = min(max(newValue, 0), 10)
        }
      }
    }
    1. Amplifier์€ ๋ฃจํŠธ ํด๋ž˜์Šค์ด๋‹ค.
    2. _volume์€ private์œผ๋กœ ์„ ์–ธ๋˜์–ด์žˆ๋Š”๋ฐ, ์ด๋Š” ์™ธ๋ถ€ ์œ ์ €๋กœ๋ถ€ํ„ฐ ์ˆจ๊ฒจ์ ธ์žˆ๋‹ค. ์•ž์— underscore๋ฅผ ๋ถ™์ธ ๊ฒƒ์€ private ๊ตฌํ˜„ ์‚ฌํ•ญ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ฐ•์กฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. convention์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง€ํ‚ค๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
    3. stored property isOn์€ ์™ธ๋ถ€ ์œ ์ €๋“ค์ด ์ฝ์„ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ํŽธ์ง‘ํ•  ์ˆ˜๋Š” ์—†๋‹ค. (Read Only) private(set) ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.
    4. plugIn()๊ณผ unplug()๋Š” isOn์˜ ์ƒํƒœ์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.
    5. computed property์ธ volume์€ _volume์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋‹ค.
    6. getter์—์„œ isOn์ด ๊บผ์ ธ์žˆ๋‹ค๋ฉด ๋ณผ๋ฅจ์„ 0์œผ๋กœ ์ค„์ธ๋‹ค.
    7. setter์—์„œ ๋ณผ๋ฅจ์€ ํ•ญ์ƒ 0์—์„œ 10์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๊ณ ์ •๋œ๋‹ค.
      ใ…ค
      ใ…ค

    Polymorphism

    ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์  ์ค‘ ํ•˜๋‚˜๋Š” ๋ฐ”๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ๋ฐ”๋กœ ๋‹คํ˜•์„ฑ (Polymorphism)์ด๋ผ๊ณ  ํ•œ๋‹ค.

    class Band {
      let instruments: [Instrument]
    
      init(instruments: [Instrument]) {
        self.instruments = instruments
      }
    
      func perform(_ music: Music) {
        for instrument in instruments {
          instrument.perform(music)
        }
      }
    }
    • Band ํด๋ž˜์Šค๋Š” ์ด๋‹ˆ์…œ๋ผ์ด์ €์—์„œ ์ดˆ๊ธฐํ™”ํ•œ instruments ๋ฐฐ์—ด์„ stored property๋กœ ๊ฐ–๊ณ  ์žˆ๋‹ค. band๋Š” perform ๋ฉ”์†Œ๋“œ์—์„œ ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ instrument๋ฅผ ๋ชจ๋‘ ๋Œ๋ฉด์„œ perform ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
      ใ…ค
      ใ…ค
    let instruments = [piano, acousticGuitar, electricGuitar, bassGuitar]
    let band = Band(instruments: instruments)
    band.perform(music)
    • ๋จผ์ € instruments๋ฅผ ์ •์˜ํ•œ ํ›„, Band ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  instruments ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
    • ๊ทธ๋ฆฌ๊ณ  perform(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์‹ค์ œ Band ๋‚ด์˜ instruments๋Š” [Instrument] ํƒ€์ž…์ด์ง€๋งŒ, ๊ฐ instrument๋Š” ๊ฐ๊ฐ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ์ž‘๋™ํ•œ๋‹ค. ์ด๊ฒŒ ๋ฐ”๋กœ ๋‹คํ˜•์„ฑ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
      ใ…ค
      ใ…ค

    Access Control

    Access control์€ ๋‹ค๋ฅธ ์†Œ์Šค ํŒŒ์ผ๊ณผ ๋ชจ๋“ˆ์— ์žˆ๋Š” ์ฝ”๋“œ์—์„œ ์ผ๋ถ€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œํ•œํ•œ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ์„ธ๋ถ€ ๊ตฌํ˜„ ์‚ฌํ•ญ์„ ์ˆจ๊ธฐ๊ฑฐ๋‚˜ ํ•ด๋‹น ์ฝ”๋“œ์— ์ ‘๊ทผํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    Swift๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 5๊ฐ€์ง€ ๋ ˆ๋ฒจ์˜ ์ ‘๊ทผ ์ œ์–ด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

    • private
      • ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • fileprivate
      • ๊ฐ™์€ ์†Œ์Šค ํŒŒ์ผ ๋‚ด์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ (.swift)
    • internal
      • ๊ฐ™์€ ๋ชจ๋“ˆ์ด๋‚˜ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • public
      • ๋ชจ๋“ˆ ์™ธ๋ถ€์—์„œ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • open
      • ๋ชจ๋“ˆ ์™ธ๋ถ€ ์–ด๋””์—์„œ๋‚˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์™ธ๋ถ€์—์„œ subclass ๋˜๋Š” override๋„ ๊ฐ€๋Šฅ
        ใ…ค
        ใ…ค

    ์ถ”๊ฐ€์ ์œผ๋กœ final ํ‚ค์›Œ๋“œ๋Š” subclass, override๋„ ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ ํด๋ž˜์Šค๋‚˜ ์†์„ฑ, ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๊ธฐ๋ณธ์ ์œผ๋กœ internal access๋กœ ์„ค์ •๋œ๋‹ค.
    ใ…ค
    ใ…ค

    Object Oriented Programming in Swift

    ๋Œ“๊ธ€

Designed by Tistory.