您的位置:首页 > 移动开发 > Swift

Swift_继承

2017-03-03 16:10 120 查看

Swift_继承

点击查看源码

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()

*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: