Created
June 5, 2014 17:19
-
-
Save satooshi/0a25782a15dc215421b7 to your computer and use it in GitHub Desktop.
hello Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol Greeter { | |
| func greet() -> String | |
| } | |
| protocol エンジニャー { | |
| func meo() | |
| } | |
| class Person: Greeter { | |
| var name: String = "kitamura" | |
| var age: Int? | |
| init() {} | |
| init(name: String) { | |
| self.name = name | |
| } | |
| func greet() -> String { | |
| return name | |
| } | |
| func hello() { | |
| println("hello " + greet()) | |
| } | |
| } | |
| // extend Person | |
| extension Person: エンジニャー { | |
| func meo() { | |
| println(name + " にゃーん") | |
| } | |
| } | |
| // extend again | |
| extension Person { | |
| convenience init(name: String, age: Int) { | |
| self.init(name: name) | |
| self.age = age | |
| } | |
| } | |
| var person = Person() | |
| println(person.greet()) // kitamura | |
| person.hello() // hello kitamura | |
| // set name | |
| person = Person(name: "hoge") | |
| person.hello() // hello hoge | |
| // set age | |
| person = Person(name: "satoshi", age: 30) | |
| person.meo() // satoshi にゃーん |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment