點擊查看源碼git
func testInheritance() { //基類 class Base { var count = 0.0 var description: String { return "count:\(count)" } //可繼承 func inherited() { } //防止繼承 final func preventing() { //若是不想子類繼承 可在類 屬性或方法前添加final } } //子類 class Subclass: Base { //繼承的屬性和方法前都有override override var count:Double { didSet { print("\(#function)") } } override var description: String { return "\(#function)" + super.description } override func inherited() { print("\(#function)") } } let subclass = Subclass() subclass.count = 10 print("\(subclass.description)") subclass.inherited() /* print count descriptioncount:10.0 inherited() */ }