開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta
- Swift (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド: 独自の構造を使う)、金庫破り(p.242)をSwiftで考えてみる。
金庫破り(p.242)
コード(Xcode)
main.swift
// // main.swift // sample242 // // Created by kamimura on 8/29/14. // Copyright (c) 2014 kamimura. All rights reserved. // import Foundation println("構造体") struct Swag { let description: String let value:Double } struct Combination { let swag:Swag let sequence:String } struct Safe { let numbers:Combination let make:String } let gold:Swag = Swag(description: "GOLD!", value: 1000000.0) let numbers:Combination = Combination(swag: gold, sequence: "6502") let s:Safe = Safe(numbers: numbers, make: "RAMACON250") println(s.numbers.swag.description) println("class") class Swag1 { let description:String let value:Double init(description:String, value:Double) { self.description = description self.value = value } } class Combination1:Swag1 { let sequence:String init(sequence:String, description:String, value:Double) { self.sequence = sequence super.init(description: description, value: value) } } class Safe1:Combination1 { let make:String init(make:String, sequence:String, description:String, value:Double) { self.make = make super.init(sequence: sequence, description: description, value: value) } } let s1:Safe1 = Safe1(make: "RAMACON250", sequence: "6502", description: "GOLD!", value: 1000000.0) println(s1.description)
入出力結果(Console Output, Terminal)
構造体 GOLD! class GOLD! Program ended with exit code: 0
0 コメント:
コメントを投稿