Skip to content

Instantly share code, notes, and snippets.

@LeeKahSeng
Last active August 13, 2017 14:51
Show Gist options
  • Save LeeKahSeng/57c5a2b83373bdbabfcee7ddddd33e81 to your computer and use it in GitHub Desktop.
Save LeeKahSeng/57c5a2b83373bdbabfcee7ddddd33e81 to your computer and use it in GitHub Desktop.
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