Created
November 19, 2022 13:38
-
-
Save EvolvingParty/ffbb7cbcac0ba47f8b056c3337ddabab to your computer and use it in GitHub Desktop.
DAY 12. 100 Days of SwiftUI – Hacking with Swift. Checkpoint 7
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
import Cocoa | |
///Make a class hierarchy for animals, starting with Animal at the top, then Dog and Cat as subclasses, then Corgi and Poodle as subclasses of Dog, and Persian and Lion as subclasses of Cat. | |
///But there’s more: | |
///The Animal class should have a legs integer property that tracks how many legs the animal has. | |
///The Dog class should have a speak() method that prints a generic dog barking string, but each of the subclasses should print something slightly different. | |
///The Cat class should have a matching speak() method, again with each subclass printing something different. | |
///The Cat class should have an isTame Boolean property, provided using an initializer. | |
class Animal { | |
var numberOfLegs: Int | |
init(numberOfLegs: Int) { | |
self.numberOfLegs = numberOfLegs | |
print("Animal with \(numberOfLegs) legs created.") | |
} | |
} | |
class Dog: Animal { | |
func speak() { | |
print("Woof! Woof! Woof!") | |
} | |
} | |
class Corgi: Dog { | |
override func speak() { | |
print("Ruff! Ruff! Ruff!") | |
} | |
} | |
class Poodle: Dog { | |
override func speak() { | |
print("Yip! Yip! Yip!") | |
} | |
} | |
class Cat: Animal { | |
var isTame: Bool | |
init(isTame: Bool, numberOfLegs: Int) { | |
self.isTame = isTame | |
super.init(numberOfLegs: numberOfLegs) | |
} | |
func speak() { | |
print("Meeeeeoooooow") | |
} | |
} | |
class Persian: Cat { | |
override func speak() { | |
print("Purr, Purr, Purr.") | |
} | |
} | |
class Lion: Cat { | |
override func speak() { | |
print("Rrrraaaawww!") | |
} | |
} | |
var simba = Lion(isTame: false, numberOfLegs: 4) | |
var neela = simba | |
print(neela.isTame) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment