Swift类中的错误:在super.init调用时未初始化属性
我有两节课,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)
为什么我必须self.sideLength
在打电话前设置super.init
?
PIPIONE