開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta
- Swift (プログラミング言語)
Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 5(Understanding Objects: A trip to Objectville)、SHARPEN YOUR PENCIL(p.194)をSwiftで考えてみる。
SHARPEN YOUR PENCIL(p.194)
コード(Xcode)
main.swift
// // main.swift // sample194 // // Created by kamimura on 8/13/14. // Copyright (c) 2014 kamimura. All rights reserved. // import Foundation class File { var level:String var opened:Int = 0 var password:Int var contents:String init(level:String, password:Int, contents:String) { self.level = level self.password = password self.contents = contents } func getSecret(secretPassword:Int) -> String { if secretPassword == password { return contents } else { return "Invalid password! No secret for you" } } func setSecret(secretPassword:Int, secret:String) { if secretPassword == password { contents = secret } } } let superSecretFile = File(level: "classified", password: 2, contents: "Dr. Evel's next meeting is in Detroit.") var secret = superSecretFile.getSecret(2) println(secret) superSecretFile.setSecret(2, secret: "Dr. Evel's next meething is in Philadelphia") secret = superSecretFile.getSecret(2) println(secret)
入出力結果(Console Output)
Dr. Evel's next meeting is in Detroit. Dr. Evel's next meething is in Philadelphia Program ended with exit code: 0
0 コメント:
コメントを投稿