我有这个超类:
class Car {
var made : String
var model : String
var year : Int
var litres : Double
var hp : Int
init (made : String , model : String , year : Int , litres : Double , hp : Int) {
self.made = made
self.model = model
self.year = year
self.litres = litres
self.hp = hp
}
func carDescription () -> String {
return "The made of the car is \(made), model is \(model). The year is \(year), with litres of \(litres) and a horsepower of \(hp)"
} }
这个子类:
class SuperCar : Car {
var doors : String
override func carDescription() -> String {
super.carDescription()
return "The made of the car is \(made), model is \(model). The year is \(year), with litres of \(litres) and a horsepower of \(hp). The doors of this car opens like \(doors)"
}
init(made: String, model: String, year: Int, litres: Double, hp: Int , doors : String){
// the line below gets the "Editor Placeholder in source file
super.init(made: String, model: String, year: Int, litres: Double, hp: Int)
self.made = made
self.model = model
self.year = year
self.litres = litres
self.hp = hp
self.doors = doors
}}
我已经看过一些教程(也许是旧教程)并且他们教导子类中的init()并没有任何参数。但是我现在使用的Xcode需要我输入所有的超类'参数。
输入后,我在源文件"中获得了#34;编辑器占位符。警告和代码无法正确编译。
答案 0 :(得分:1)
代码中有两个主要错误。
super
super
初始值设定项中传递值,而不是类型 最后,您可以在super
电话后省略所有内容。
init(made: String, model: String, year: Int, litres: Double, hp: Int , doors : String){
self.doors = doors
super.init(made: made, model: model, year: year, litres: litres, hp: hp)
}