Last active
August 13, 2017 14:51
-
-
Save LeeKahSeng/57c5a2b83373bdbabfcee7ddddd33e81 to your computer and use it in GitHub Desktop.
This file contains 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 Animal { | |
var name: String { get } | |
func walk() | |
} | |
class Cow: Animal { | |
let name: String | |
init(withName name: String) { | |
self.name = name | |
} | |
func walk() { | |
print("\(name) is walking in the farm.") | |
} | |
} | |
class Tiger: Animal { | |
let name: String | |
init(withName name: String) { | |
self.name = name | |
} | |
func walk() { | |
print("\(name) is walking in the jungle.") | |
} | |
} | |
// Create concrete Animal instant | |
let myTiger = Tiger(withName: "My Tiger") | |
let myCow = Cow(withName: "My Cow") | |
let animalArray: [Animal] = [myTiger, myCow] | |
for animal in animalArray { | |
animal.walk() | |
// Output: | |
// My Tiger is walking in the jungle. | |
// My Cow is walking in the farm. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment