我有两个类,Shape
和Square
class Shape {
var numberOfSides = 0
var name: String
init(name:String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: Shape {
var sideLength: Double
init(sideLength:Double, name:String) {
super.init(name:name) // Error here
self.sideLength = sideLength
numberOfSides = 4
}
func area () -> Double {
return sideLength * sideLength
}
}
通过上面的实现,我得到了错误:
property 'self.sideLength' not initialized at super.init call
super.init(name:name)
为什么我必须在调用super.init
之前设置self.sideLength
?