Swift 继承Inheritance

前端之家收集整理的这篇文章主要介绍了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 asfinal. Do this by writing thefinalmodifier before the method,or subscript’s introducer keyword (such asfinal var,final func,monospace; word-wrap:break-word">final class func,andfinal subscript).

Any attempt to override a final method,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 thefinalmodifier before theclasskeyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.

原文链接:https://www.f2er.com/swift/324408.html

猜你在找的Swift相关文章