class Rectangle {
var width : Double
var height : Double
init(width : Double, height : Double) { ①
self.width = width
self.height = height
}
init(W width : Double,H height : Double) { ②
self.width = width
self.height = height
}
init(length : Double) { ③
self.width = length
self.height = length
}
init() { ④
self.width = 640.0
self.height = 940.0
}
}
var rectc1 = Rectangle(width : 320.0, height : 480.0) ⑤
println("长方形:\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0) ⑥
println("长方形:\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0) ⑦
println("长方形3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle() ⑧
println("长方形4:\(rectc4.width) x \(rectc4.height)")
struct Rectangle {
var width : Double
var height : Double
init(width : Double, height : Double) { ①
self.width = width
self.height = height
}
init(W width : Double,H height : Double) { ②
self.width = width
self.height = height
}
init(length : Double) { ③
self.init(W : length, H : length)
}
init() { ④
self.init(width: 640.0, height: 940.0)
}
}
var rectc1 = Rectangle(width : 320.0, height : 480.0) ⑤
println("长方形:\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0) ⑥
println("长方形:\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0) ⑦
println("长方形3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle() ⑧
println("长方形4:\(rectc4.width) x \(rectc4.height)")
class Rectangle {
var width : Double
var height : Double
init(width : Double, height : Double) { ①
self.width = width
self.height = height
}
init(W width : Double,H height : Double) { ②
self.width = width
self.height = height
}
convenience init(length : Double) { ③
self.init(W : length, H : length)
}
convenience init() { ④
self.init(width: 640.0, height: 940.0)
}
}
var rectc1 = Rectangle(width : 320.0, height : 480.0) ⑤
println("长方形:\(rectc1.width) x \(rectc1.height)")
var rectc2 = Rectangle(W : 320.0, H : 480.0) ⑥
println("长方形:\(rectc2.width) x \(rectc2.height)")
var rectc3 = Rectangle(length: 500.0) ⑦
println("长方形3:\(rectc3.width) x \(rectc3.height)")
var rectc4 = Rectangle() ⑧
println("长方形4:\(rectc4.width) x \(rectc4.height)")
第③行的self.init(W : length, H : length)语句是在横向调用第②行定义的构造器代理,第④行的self.init(width: 640.0, height: 940.0)语句是在横向调用第①行定义的构造器代理。
欢迎关注智捷iOS课堂微信公共平台