创建一个基类:Vehicle.swift
import Foundation
//
// @brief 定义一个超类(车)
//
class Vehicle {
var numberOfWheels: Int
var maxPassengers: Int
// designated initializer
init() {
numberOfWheels = 0
maxPassengers = 2
println("call vehicle designated initializer")
self.nothingToDo()
}
func description() {
println("Vehicle has (\numberOfWheels) wheels and can hold \(maxPassengers) passengers at most")
}
// 使用@final防止被重写
@final func nothingToDo() {
println("call nothingToDo() and it is used @final to avoid overriding")
}
}
再创建一个子类Bicycle.swift:
import Foundation
//
// @brief 定义自行车类
//
class Bicycle : Vehicle {
// override property
override var numberOfWheels: Int {
get { // 重写属