ios - 麻烦帮忙看下这段swift代码哪里有错
PHPz
PHPz 2017-04-17 16:01:54
0
1
392

编程初学者,学习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)

PHPz
PHPz

学习是最好的投资!

reply all(1)
Ty80

I copied your code and PG can run it. Please see the comments for specific questions

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)"
        // 你这里 A前面的引号有问题
    }
    
}

let test = Circle(halfLength: 2.0, name: "sss")
// name变量的littleCircle 是个没定义的变量 是不是个字符串呀~
test.area()
test.simpleDescription()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template