Skip to content

Instantly share code, notes, and snippets.

@nmbr73
Last active September 28, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmbr73/5265fe965130cf179e97cb9eb0eb24bb to your computer and use it in GitHub Desktop.
Save nmbr73/5265fe965130cf179e97cb9eb0eb24bb to your computer and use it in GitHub Desktop.
Day 12, Checkpoint 7 of #100DaysOfSwiftUI ... nothing special here, I think ... ?!?
class Animal {
let legs = 4
}
class Dog: Animal {
func speak() {
print("bark")
}
}
final class Corgi: Dog {
override func speak() {
print("woof")
}
}
final class Poodle: Dog {
override func speak() {
print("bowbow")
}
}
class Cat: Animal {
let isTame: Bool
init(isTame: Bool = true) {
self.isTame = isTame
}
func speak() {
print("meow")
}
}
final class Persian: Cat {
override func speak() {
print("purr")
}
}
final class Lion: Cat {
init() {
super.init(isTame: false)
}
override func speak() {
print("roar")
}
}
// Some best friends:
let dog = Dog()
let corgi: Corgi
corgi = Corgi()
dog.speak()
corgi.speak()
Poodle().speak()
// The cat is a Cat but that may be different cats:
var cat: Cat
cat = Cat() ; print( "\(type(of: cat)) says:" ) ; cat.speak()
cat = Persian() ; print( "\(type(of: cat)) says:" ) ; cat.speak()
cat = Lion() ; print( "\(type(of: cat)) says:" ) ; cat.speak()
// Once a Persian, always a Persian:
var moggie: Persian
moggie = Persian()
// moggie = Cat()  ⚡️
// moggie = Lion() ⚡️
print("Thank goodness our moggie has \(moggie.legs) legs.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment