/**
重写方法
*/
class Human {
var name: String = ""
var id: Int = 0
func eat() -> Void {
print("eat")
}
func drink() -> Void {
print("drink")
}
func sleep() -> Void {
print("sleep")
}
}
// 子类 : 基类
class Woman: Human {
func birth() -> Void {
print("birth")
}
// 如果重写方法 需要在最前面加上关键字,override
// private
override func eat() {
print("\(name)Woman eat")
}
func eatandSleep() -> Void {
// 先在当前类中寻找eat,如果没有再在父类中寻找
eat()
super.sleep()
birth()
}
}
let h = Human.init()
let w = Woman.init()
w.name = "Tom"
w.id = 0987666
w.eat()
w.sleep()
w.birth()
h.eat()
h.sleep()
原文链接:https://www.f2er.com/swift/322394.html