2014年9月25日木曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 12(Creating object: Advanced Object Construction)、EXERCISE(p.531)をSwiftで考えてみる。

EXERCISE(p.531)

コード(Xcode)

main.swift

//
//  main.swift
//  sample531
//
//  Created by kamimura on 9/25/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

struct Coffee : Printable {
    let roast:String
    let ounces:Int
    var size:String {
        switch ounces {
        case 6:
            return "small"
        case 12:
            return "medium"
        case 16:
            return "large"
        default:
            return "?"
        }
    }
    var description:String { return "You've ordered a \(size) \(roast) coffee."}
}

let house_blend:Coffee = Coffee(roast: "House Blend", ounces: 12)
let dart_roast:Coffee = Coffee(roast: "Dart Roast", ounces: 16)

println(house_blend)
println(dart_roast)

入出力結果(Console Output)

You've ordered a medium House Blend coffee.
You've ordered a large Dart Roast coffee.
Program ended with exit code: 0

0 コメント:

コメントを投稿