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

Mobile Programming Struct

yuichiro_takahashi
December 09, 2018
120

Mobile Programming Struct

Mobile Programmingの授業で使うスライドです

yuichiro_takahashi

December 09, 2018
Tweet

Transcript

  1. ςΩετ ࣮ࡍͷίʔυ // structͷఆٛ struct Pokemon { let name: String

    var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } } // ΠϯελϯεԽ let pikachu = Pokemon(name: "ϐΧνϡ΢", level: 100) // ϝιουݺͼग़͠ pikachu.displayStatus()
  2. ςΩετ ࣮ࡍͷίʔυ // structͷఆٛ struct Pokemon { let name: String

    let ability: Ability var level: Int var health: Int func displayStatus() { print("name: \(self.name)") print("Lv: \(self.level)") print("HP: \(self.health)") } // mutating Λ͚ͭΔ͜ͱͰ // ͜ͷϝιουͷதͰͷΈpropertyΛॻ͖׵͑Մೳ mutating func takeDamage(from ability: Ability) { self.health -= ability.damage } } // ΠϯελϯεԽ let ability = Ability(name: "ϘϧοςΧʔ", damage: 120) let ability2 = Ability(name: "΅͏;͏", damage: 120) // Մมʹ͍ͨ͠Struct͸varͰఆٛ͢Δ var pikachu = Pokemon(name: "ϐΧνϡ΢", ability: ability, level: 100, health: 200) var pidgey = Pokemon(name: "ϙοϙ", ability: ability2, level: 100, health: 200) pidgey.takeDamage(from: pikachu.ability)
  3. ςΩετ ࣮ࡍͷίʔυ protocol Pokemon { var name: String { get

    } var ability: Ability { get } var level: Int { get } var health: Int { get set } func displayStatus() func doAbility<T: Pokemon>(with pokemon: T) } struct Pikachu: Pokemon { let name: String let ability: Ability let level: Int var health: Int func displayStatus() { // লུ... } func doAbility<T>(with pokemon: T) where T : Pokemon { // লུ... } }