/**
13.7-关于便利构造方法“重写”的讨论.mp4
重写成一个便利构造方法
Swift语言中便利构造方法不能重写。因为它是横向的
*/
class Human {
let name: String
var age: Int
init(name: String,age: Int) {
self.name = name
self.age = age
}
convenience init() {
self.init(name: "",age: 0)
}
}
class Woman: Human {
var haveBaby: Bool = false
// 派生类中定义一个构造方法,它跟父类中的某个指定构造方法一样
// override init(name: String,age: Int) {
// haveBaby = false
// super.init(name: name,age: age)
// }
init(name: String,age: Int,haveBaby: Bool) {
self.haveBaby = haveBaby
super.init(name: name,age: age)
}
// convenience override init(name: String,0)"> // self.init(name: name,age: age,haveBaby: false)
// }
// Initializer does not override a designated initializer from its superclass,在Swift语言便利构造方法中不存在重写
// convenience override init() {
// self.init(name: "",age: 0,0)"> // }
// 这个是当前类中的便利构造方法
convenience init() {
self.init(name: "",age: 0,haveBaby: false)
}
}
var w0 = Woman.init(name: "d",haveBaby: true)
var w1 = Woman.init()
原文链接:https://www.f2er.com/swift/322371.html