PART_ONLY 继承
- 语法格式
class Cat: Animal {
// class body
}
- 子类自动获得父类的所有特性:属性、方法等,同时可自定义新的特性
- 重写
- 访问父类的方法,属性及下标
-
super.someMethod()
-
super.someProperty
-
super[someIndex]
- 重写方法
ovveride func eat() { ... }
- 重写属性
- 重写属性的 getter 和 setter(存储型、计算型属性皆可)
- 可将只读属性重写为读写属性,不可将读写属性重写为只读属性
// 重写 getter
ovveride var desc: String {
return super.desc + "detail"
}
- 重写属性观察器
- 不可为继承来的常量存储属性或只读计算型属性添加属性观察器
- 不可同时提供重写的 setter 和重写的属性观察器
// 重写属性观察器
override var currentSpeed: Double {
didSet {
gear = Int(currentSpeed / 10.0) + 1
}
}
- 防止重写
-
final var
-
final func
-
final class func
-
final subscript
-
final class
:不可继承类