编程初学者,学习swift中,官方教材这边要求创建一个新类Circle继承NamedShape,构造器接收两个参数,一个是半径一个是名称,在子类 Cir cle 中实现 area() 和 simpleDescription() 方法。
但是在playground中一直提示出错,undetermined string literal和invalid character in source file 还有 use of unresolved identifier 'littleCircle'
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 halfLength:Double
init (halfLength:Double,name:String){
self.halfLength = halfLength
super.init(name:name)
numberOfSides = 4
}
func area() -> Double{
return 3*halfLength*halfLength
}
override func simpleDescription()->String{
return “A circle with halfLength of \(halfLength)"
}
}
let test = Circle(halfLength: 2.0, name: littleCircle)
我複製了你的程式碼 PG可以運行 具體問題 看註解