Nick Walter Challenges (Part 6)
Next part - all linked to classes! We have inheritance and initialization - two areas I do need a clearer understanding. Let's go!
Start time - 18:09
Inheritance - Subclassing
Start time - 18:09
Inheritance - Subclassing
class Animal {
var name = ""
var age = ""
func sayHello() {
print("Hello! I am an animal!")
}
}
class Mammal: Animal {
var hairColour = ""
}
let snake = Animal()
let dog = Mammal()
dog.hairColour = "brown"
dog.hairColour
class Monkey: Mammal {
var thumbSize = 0
}
let george = Monkey()
george.thumbSize
george.sayHello()
Nothing new here to be honest but nice to recap!
Overriding
Use of the override keyword if you use the same name of the function from the super class but change its functionality in some way. Easy.
Use of the super. option - this means you are accessing the superclass to use that description. This means you are using elements of the superclass, then adding something else - otherwise you wouldn't bother creating the function in the first place!
If there is a chain of inheritance, then you would use the superclass immediately above. If it doesn't have that, then the one above that!
Use of 'final' - this means that nothing can ever override it.
Challenge
OK - these all look fine. Let's go!
class TVShow {
var name = ""
var year = 2018
final func themeSongPlay() -> String {
return "La la la la la!"
}
}
class Comedy: TVShow {
/*override func themeSongPlay() -> String {
return super.themeSongPlay() + " Then some silly noises!"
}*/
}
These were so easy! TBC...
Paused time 18:28 (19 minutes so far)
Restart time - 20:52
So time to check the solution. I literally did the above in a few minutes - quickest yet I think!
Initializers
class Dog {
var name: String
var age = 0
init(name: String) {
self.name = name
}
}
let myDog = Dog(name: "Fido")
myDog.name
I've gone further than Nick for this first bit, as it's more useful to be able to create and customise the value upon initialisation.
A reminder about structs - they have member-wise initializers. So more options come up during the init phase.
Failable Initializers
This is useful as I don't actually remember doing this before!
init?(name: String, age: Int) {
self.name = name
self.age = age
if name.isEmpty {
return nil
}
if age < 0 {
return nil
}
}
So the ? Makes the init failable, then if statements are needed to show a condition. Simple stuff.
if name.isEmpty || age < 0 {
return nil
}
That is actually neater.
Deinitialized
deinit {
print("Incorrect infomation used")
}
No problem. Now challenge time! Last one for the night!
So 1) looks fine - a few bits of code needed. The second one should also be OK. Failable again for 3) and deinit pretty easy.
class Quote {
var author: String
var text: String
init?(author: String, text: String) {
self.author = author
self.text = text
if author.isEmpty || text.isEmpty {
return nil
}
}
init?(text: String) {
self.text = text
author = "Unknown"
if text.isEmpty {
return nil
}
}
deinit {
print("Goodbye!")
}
}
All done - again pretty easy stuff!
Finish time - 21:26 (34 more minutes; 53 in total)
And there we have it - that's everything for classes! Lots of elements to it - I had forgotten how many steps there were to understanding everything. From properties to inheritance to initializers to deinit, to failable - lots of elements. Great to go over and it shows that I DO have plenty of knowledge that I can apply.
Comments
Post a Comment