2014年8月27日水曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 7(Types, Equality, Conversion and All That Jazz: Serious types))、IN THE LABORATORY, AGAIN(p.305)をSwiftで考えてみる。

IN THE LABORATORY, AGAIN(p.305)

コード(Xcode)

main.swift

//
//  main.swift
//  sample305
//
//  Created by kamimura on 8/27/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

class Duck {
    let sound:String
    
    init(sound:String) {
        self.sound = sound
    }
    
    func quack() {
        println(self.sound)
    }
}

struct Duck1 {
    let sound:String
    func quack() { println(self.sound) }
}

println("クラス")
var toy:Duck? = Duck(sound: "quack quak")
var toy1:Duck?

println("toy")
println(toy)
toy?.quack()
println(toy is Duck)

println("toy1")
println(toy1)
toy1?.quack()
println(toy1 is Duck)

println("構造体")
var toy2:Duck1? = Duck1(sound: "quack quack")
var toy3:Duck1?

println("toy2")
println(toy2)
toy2?.quack()
println(toy2 is Duck1)

println("toy3")
println(toy3)
toy3?.quack()
println(toy3 is Duck1)

入出力結果(Console Output)

クラス
toy
Optional(_TtC9sample3054Duck)
quack quak
true
toy1
nil
false
構造体
toy2
Optional(_TtV9sample3055Duck1)
quack quack
true
toy3
nil
false
Program ended with exit code: 0

0 コメント:

コメントを投稿