前端之家收集整理的这篇文章主要介绍了
从零开始自学Swift(八),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
继承情况下子类的构造函数
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()