继承情况下子类的构造函数
class MacBook{
var price:Double
var color:String
init(price:Double,color:String){
self.price=price
self.color=color
}
}
class MacBookAir : MacBook{
var weight:Double
init(weight:Double){
self.weight=weight
super.init(price: 12,color:"red")
}
}
var oneMacBookAir=MacBookAir(weight: 12.4)
class MacBook{
var price:Double
var color:String
init(price:Double,color:String){
self.price=price
self.color=color
print("这是父类的构造函数")
}
}
class MacBookAir : MacBook{
var weight:Double?
init(weight:Double){
self.weight=weight
super.init(price: 12,color:"red")
}
override init(price: Double,color: String) {
print("父类的构造函数已被重写")
self.weight=nil
super.init(price: price,color: color)
}
func toString(){
print("价格为:\(price),颜色为:\(color),重量为:\(weight)")
}
}
var twoMacBookAir=MacBookAir(price: 13,color: "white")
twoMacBookAir.toString()