Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile Programming Inheritance

Mobile Programming Inheritance

モバイルプログラミングの授業で使うスライドです

yuichiro_takahashi

November 26, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ ܧঝͬͯʁ ▸ Ϋϥε͕࣋ͭػೳͷҰͭ ▸ ਌͕࣋ͭϓϩύςΟ΍ϝιουΛड͚ܧ͙ ▸ ܧঝݩͷΫϥεΛ਌Ϋϥε(super class) ▸

    ܧঝઌͷΫϥεΛࢠΫϥε(sub class)ͱ͍͏ ▸ จ๏͸ class SubClass: Super Class { ... } ▸ swift Ͱ͸࢖͏৔ॴ͸ݶΒΕΔ ▸ جຊతʹ͸ϙϦϞʔϑΟζϜ(ଟଶੑ)ͷͨΊʹ࢖ΘΕΔ
  2. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    String var level: Int = 0 init(name: String, level: Int, ability: String = "͍ͨ͋ͨΓ") { self.name = name self.level = level self.ability = ability } func displayStatus() { print("\(self.name)ͷϨϕϧ͸\(self.level)Ͱ͢ɻ") } func doAbility(to pokemon: Pokemon) { print("\(self.name)͸\(pokemon.name)ʹ\(self.ability)ͨ͠ʂ") } } class Metagross: Pokemon { init(level: Int) { super.init(name: "ϝλάϩε", level: level, ability: "ίϝοτύϯν") } }
  3. ςΩετ ਌ΫϥεͱࢠΫϥε ▸ جຊతʹࢠΫϥε͔Β਌ΫϥεͷϓϩύςΟ΍ϝιου͸શͯΞΫ ηεՄೳ ▸ ͦͷࡍ͸self ͷ୅ΘΓʹsuperΛ࢖͏ ▸ ࢠΫϥεͰ͸਌ΫϥεͷϝιουΛॻ͖׵͑Δ͜ͱ͕Ͱ͖Δ

    ▸ ͜ΕΛϝιουͷΦʔόʔϥΠυͱݴ͏ ▸ ࢠΫϥεʹ͸৽͘͠ϝιου΍ϓϩύςΟΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ ▸ ࢠΫϥε͸਌Ϋϥεͷܕ΋Ҿ͖ܧ͙
  4. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String // pokemon's

    name (ex pikachu) fileprivate(set) var level: Int // pokemon's level fileprivate(set) var health: Int = 1 // pokemon's hit point init(name: String, level: Int) { self.name = name self.level = level self.health = level * 2 } func displayStatus() { print("Name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } func doAbility() { print("\(self.name)ͷ͍ͨ͋ͨΓʂ") } } class Slowpoke: Pokemon { init(level: Int) { // ਌ΫϥεͷϝιουΛݺͼग़͢ super.init(name: "Ϡυϯ", level: level) } // ਌Ϋϥεʹఆٛ͞Ε͍ͯΔϝιουΛॻ͖׵͑Δ override func doAbility() { print("\(self.name)ͷͶΜΓ͖ʂ") } } let slowpoke: Pokemon = Slowpoke(level: 10)
 
 slowpoke.displayStatus() slowpoke.doAbility() // ϠυϯͷͶΜΓ͖ʂͱදࣔ͞ΕΔ(͍ͨ͋ͨΓͰ͸ͳ͍)