Swift 继承Inheritance
2016-03-05 10:08
381 查看
Swift中的类的继承,可以继承类的属性和方法,或是其他特性.子类也可以重写父类的方法或是属性.
定义一个父类:
3.1重写父类的方法
防止重写可以在属性,方法或是类,前面加上关键字final.
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the
before the method, property, or subscript’s introducer keyword (such as
Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
You can mark an entire class as final by writing the
before the
in its class definition (
定义一个父类:
class Vehicle { var currentSpeed = 0.0; var description:String{ return "traverling at \(currentSpeed) miles per hour" } func makeNoise(){ } }
/// 创建一个Vehicle 对象 let somevehicle = Vehicle() somevehicle.currentSpeed = 5; print("Vehicle:\(somevehicle.description)") // Vehicle:traverling at 5.0 miles per hour1.创建一个子类
/// 创建一个子类 继承于Vehicle class Bicycle: Vehicle { var hasBasket = false } let bicycle = Bicycle(); bicycle.hasBasket = true bicycle.currentSpeed = 15; print("Bicycle:\(bicycle.description)") // Bicycle:traverling at 15.0 miles per hour2.创建一个继承于Bicycle的子类
/// 创建一个子类 继承于Bicycle class Tandem: Bicycle { var currentNumberOfPassengers = 0 } let tandem = Tandem() tandem.hasBasket = true tandem.currentNumberOfPassengers = 2 tandem.currentSpeed = 22.0 print("Tandem: \(tandem.description)") // Tandem: traverling at 22.0 miles per hour3.重写Overriding
3.1重写父类的方法
// 重写方法 class Train: Vehicle { override func makeNoise() { print("Choo Choo") } } let train = Train() train.makeNoise() // Choo Choo3.2 重写属性
// 重写属性 class SpeedLimitedCar: Vehicle { override var currentSpeed: Double { get { return super.currentSpeed } set { super.currentSpeed = min(newValue, 40.0) } } } let limitedCar = SpeedLimitedCar() limitedCar.currentSpeed = 60.0 print("SpeedLimitedCar: \(limitedCar.description)") // SpeedLimitedCar: traverling at 40.0 miles per hour3.3重写属性监视器
class Car: Vehicle { var gear = 1 override var description: String { return super.description + " in gear \(gear)" } } let car = Car() car.currentSpeed = 25.0 car.gear = 3 print("Car: \(car.description)") // Car: traverling at 25.0 miles per hour in gear 3 // 重写属性监视器 class AutomaticCar: Car { override var currentSpeed: Double { didSet { gear = Int(currentSpeed / 10.0) + 1 } } } let automatic = AutomaticCar() automatic.currentSpeed = 35.0 print("AutomaticCar: \(automatic.description)") // AutomaticCar: traverling at 35.0 miles per hour in gear 44.防止重写
防止重写可以在属性,方法或是类,前面加上关键字final.
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the
finalmodifier
before the method, property, or subscript’s introducer keyword (such as
final var,
final func,
final class func, and
final subscript).
Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
You can mark an entire class as final by writing the
finalmodifier
before the
classkeyword
in its class definition (
final class). Any attempt to subclass a final class is reported as a compile-time error.
相关文章推荐
- swift:CoreData简单入门(增加、查询、修改、删除)(详细讲解)
- How to create a pure Swift module
- swift基本语法
- Swift 初体验之三(终结篇),实现登陆注册
- iOS3D-Touch开发之Home Screen Quick Actions(Swift)
- Swift 2.0 异常处理--throws
- swift 学习资料网站
- Swift实现自定义AlertView,灵活添加自定义视图到AlertView,可响应事件
- Swift 实践之简单计算器
- Swift 问题
- Swift学习笔记————可选类型(Optional Type)
- Swift 基础学习(闭包)
- Swift - 使用CoreLocation获取设备方向(真实方向,磁极方向)(转帖)
- 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法
- 《从零开始学Swift》学习笔记(Day 49)——扩展声明
- 《从零开始学Swift》学习笔记(Day48)——类型检查与转换
- 《从零开始学Swift》学习笔记(Day 47)——final关键字
- 《从零开始学Swift》学习笔记(Day 46)——下标重写
- Swift语言iOS8的蓝牙Bluetooth解析(转帖)
- 《从零开始学Swift》学习笔记(Day 45)——重写方法