Last active
September 28, 2022 09:12
-
-
Save nmbr73/5265fe965130cf179e97cb9eb0eb24bb to your computer and use it in GitHub Desktop.
Day 12, Checkpoint 7 of #100DaysOfSwiftUI ... nothing special here, I think ... ?!?
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
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