/**
13.6-重写指定构造方法.mp4
是指“子类中构造方法与父类中的构造方法的参数列表一模一样。”
*/
class Human {
let name: String
var age: Int
init(name: String,age: Int) {
self.name = name
self.age = age
}
}
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,age: Int) {
// self.init(name: name,age: age,haveBaby: false)
// }
}
原文链接:https://www.f2er.com/swift/322372.html