Swift 继承Inheritance

Swift中的类的继承,可以继承类的属性和方法,或是其他特性.子类也可以重写父类的方法或是属性.

定义一个父类:

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 hour
1.创建一个子类

/// 创建一个子类  继承于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 hour
2.创建一个继承于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 hour
3.重写 Overriding
3.1重写父类的方法

// 重写方法
class Train: Vehicle {
    override func makeNoise() {
        print("Choo Choo")
    }
}
let train = Train()
train.makeNoise()
// Choo Choo
3.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 hour
3.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 4
4.防止重写

防止重写可以在属性,方法或是类,前面加上关键字final.

You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript’s introducer keyword (such as final varfinal funcfinal 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 final modifier before the class keyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值