我在这里做错了什么?
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 Circle: NamedShape{
var radius: Double
// Here it says:"Cannot override with a stored property ‘name':
var name: String
init(radius: Double, name: String) {
self.radius = radius
super.init(name: name)
}
func area(radius: Double) ->Double{
var area: Double = radius * radius * 3.14
return area
}
override func simpleDescription() -> String {
// Here it says that 'name' is ambiguous:
return "A circle by the name of (name)with the area of (area(radius))"
}
}
let test = Circle(radius:5.1,name: myCircle)
我不是 Swift 专家,但看起来您正在定义子类 (Circle) 的另一个属性,该属性恰好与其超类 (NamedShape) 中的属性同名。
您的NamedShape
的整个想法似乎是它应该是存储名称的类。那么,为什么不跳过子类中的属性name
呢?我的意思是,删除第一个错误的行。
别担心。您仍然可以在子类中引用name
。这就是定义Circle
以扩展NamedShape
的重点。