9. Torques Inc. 2015
クラス、メソッドの復習
class Human {
var name: String = "Default"
var stamina: Int = 100
var pos: CGPoint = CGPoint(x: 0, y: 0)
init( name: String ) {
self.name = name
}
func sayHello() {
println("My name is (name)! @ ((pos.x),(pos.y))“)
}
}
// -----
var me = Human(name:"Yamada")
me.sayHello()
14. Torques Inc. 2015
SwiftのProtocol
var players = [Player(name: "P0"),Player(name:
"P1"),Player(name: "P2"),Human(name: "Hiro")]
var ball = CGPoint(x: 10, y: 10)
for p in players {
p.sayHello()
if let pr = p as? PlayerProtocol {
pr.runTo(ball)
}
}
17. Torques Inc. 2015
パルプンテDelegate
protocol ParupunteProtocol {
func performParupunte()
}
class GameSystem {
}
class Hero : Human {
func sayParupunte() {
}
}
// -----
var game = GameSystem()
var hero = Hero(name: "ああああ")
hero.sayParupunte()
18. Torques Inc. 2015
パルプンテDelegate
protocol ParupunteProtocol {
func performParupunte()
}
class GameSystem: ParupunteProtocol {
func performParupunte(){
println("パルプンテ!")
}
}
19. Torques Inc. 2015
パルプンテDelegate
class Hero : Human {
var parupunteDelegate: ParupunteProtocol?
func sayParupunte() {
if( parupunteDelegate != nil ){
parupunteDelegate.performParupunte()
}
}
}
// -----
var game = GameSystem()
var hero = Hero(name: “ああああ")
hero.parupunteDelegate = game
hero.sayParupunte()
20. Torques Inc. 2015
パルプンテDelegate:全体
protocol ParupunteProtocol {
func performParupunte()
}
class GameSystem: ParupunteProtocol {
func performParupunte(){
println("パルプンテ!")
}
}
class Hero : Human {
var parupunteDelegate: ParupunteProtocol?
func sayParupunte() {
if( parupunteDelegate != nil ){
parupunteDelegate.performParupunte()
}
}
}
// -----
var game = GameSystem()
var hero = Hero(name: “ああああ")
hero.parupunteDelegate = game
hero.sayParupunte()