Hi. This is from the Swift 4.2 Apple book, page 26. How come numberOfSides variable in line 07, wasn't inside the parameter in line 04? If you create an object it'll be:
var testSquare = Square(sideLength: 3, name: testSquare)
Why was there a need to put numberOfSides = 4 on line 7, if you won't be able to put an argument/value for it when you create an object?
Did line 07 here functioned like a get set (which one get or set?) and the value 4 is in memory, ready to use? If it did, why wasn't the normal get set used to store the value 4 for later use?
Also if numberOfSides isn't in Square but is a property in it's superclass NamedShape, why wasn't super init used (like with super.init(name: name))? How's Square telling the compiler that numberOfSides is a property from its NamedShape superclass without the super keyword?
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
Thank you in advance.
God bless, Genesis 1:3