Swift学习这十七:重载(override)

 

Swift学习这十七:重载(override) 


创建一个基类:Vehicle.Swift

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2.   
  3. //  
  4. // @brief 定义一个超类(车)  
  5. //  
  6. class Vehicle {  
  7.     var numberOfWheels: Int  
  8.     var maxPassengers: Int  
  9.       
  10.     // designated initializer  
  11.     init() {  
  12.         numberOfWheels = 0  
  13.         maxPassengers = 2  
  14.         println("call vehicle designated initializer")  
  15.         self.nothingToDo()  
  16.     }  
  17.       
  18.     func description() {  
  19.         println("Vehicle has (\numberOfWheels) wheels and can hold \(maxPassengers) passengers at most")  
  20.     }  
  21.       
  22.     // 使用@final防止被重写  
  23.     @final func nothingToDo() {  
  24.         println("call nothingToDo() and it is used @final to avoid overriding")  
  25.     }  
  26. }  

再创建一个子类Bicycle.swift:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2.   
  3. //  
  4. // @brief 定义自行车类  
  5. //  
  6. class Bicycle : Vehicle {  
  7.     // override property  
  8.     override var numberOfWheels: Int {  
  9.         get { // 重写属性,调用super.propertyName  
  10.             println("overried property getter numberOfWheels")  
  11.             return super.numberOfWheels  
  12.         }  
  13.         set {  
  14.             println("overried property setter numberOfWheels")  
  15.             super.numberOfWheels = max(newValue, 40)  
  16.         }  
  17.     }  
  18.       
  19.     // initializer 默认是不会被继承的  
  20.     init() {  
  21.         super.init() // initialize super class first  
  22.         println("called designated initializer")  
  23.         numberOfWheels = 2  
  24.     }  
  25.       
  26.     // 重写方法  
  27.     override func description() {  
  28.        super.description()  
  29.           
  30.         println("func description() was called in Bicycle")  
  31.     }  
  32.       
  33. //    // 不能重写  
  34. //    override func nothingToDo() {  
  35. //          
  36. //    }  
  37. }  


在main.swift中测试:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2.   
  3. let bicycle = Bicycle()  
  4.   
  5. bicycle.numberOfWheels = 4  
  6. println(bicycle.numberOfWheels)  
  7. bicycle.maxPassengers = 84  
  8. println(bicycle.maxPassengers)  
  9. bicycle.description()  

// 测试结果数据:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. call vehicle designated initializer  
  2. call nothingToDo() and it is used @final to avoid overriding  
  3. called designated initializer  
  4. overried property setter numberOfWheels  
  5. overried property setter numberOfWheels  
  6. overried property getter numberOfWheels  
  7. 40  
  8. 84  
  9. Vehicle has (  
  10. umberOfWheels) wheels and can hold 84 passengers at most  
  11. func description() was called in Bicycle  
  12. Program ended with exit code: 0  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值