Hello. In the
init() function (line 66), inside class TriangleAndSquare, how come there's a need for the parameter label size to be initialized, and how is it being accepted as a parameter even though it's not a property in any subclass (including that class itself) or superclass?Also how is
size being accepted as an argument (lines 67 & 68) it seems, for type Square: NameShape (line 12) in its init() function (line 15), when class Square: NameShape doesn't even have size as one of it's properties too [nor is size in the superclass: NameShape (line 1)], nor is it being initialized in 'class Square: NameShape`?Shouldn't there be an underscore
_ before size in class TriangleAndSquare's init() function parameter (line 66), perhaps as init(_ size: Double, name: String) so that any argument label, when an object is created, can be accepted as its parameter?This code is from the 'GuidedTour.playground' file from their 'The Swift Programming Language 4.2' (it's in their Swift 5 version, of the same book, as well). The code is in the section: 'Classes and Objects'.
I've already digested the Apple docs and some good video tutorials on the purpose and nature of: initialization, argument, argument labels, parameters & parameter labels but I guess, I need another view/explanation on these.
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
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)."
}
}
class EquilateralTriangle: NamedShape {
var sideLength: Double = 0.0
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 3
}
var perimeter: Double {
get {
return 3.0 * sideLength
}
set {
sideLength = newValue / 3.0
}
}
override func simpleDescription() -> String {
return "An equilateral triangle with sides of length \(sideLength)."
}
}
class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
}
}
var square: Square {
willSet {
triangle.sideLength = newValue.sideLength
}
}
init(size: Double, name: String) {
square = Square(sideLength: size, name: name)
triangle = EquilateralTriangle(sideLength: size, name: name)
}
}Thank you in advance.
God bless, Revelation 21:4