Swift继承

前端之家收集整理的这篇文章主要介绍了Swift继承前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//声明一个基类

class vehicle {

var maxPassenger : Int = 0

var manufacturer : String!

func description() -> String {

return "max passenger is \(maxPassenger) and manufacturer is \(manufacturer)"

}

init () {

maxPassenger = 2

manufacturer = "Designed By Weasley"

}

}


//声明子类

class Bicycle : vehicle {

var numberofWheels : 2

var maxSpeed : Double = 30

override init() {

super.init()

2

}

override func description() -> String {

//return "numberofWheels : \(numberofWheels) maxPassenger : \(maxPassenger) with maxSpeed : \(maxSpeed) manufacturer is \(manufacturer)"

return super.description() + ";" + "numberofWheels : \(numberofWheels) maxSpeed : \(maxSpeed)"

}

}



class LimitSpeedBicycle: Bicycle {

var maxSpeed : Double {

get {

return super.maxSpeed

}

set {

super.maxSpeed = min(maxSpeed,20)

}

}

}

var bicycle = Bicycle()

println(bicycle.description())


var limit = LimitSpeedBicycle()

limit.maxSpeed = 30

println(limit.maxSpeed)



class SpeedLevel: var level = 1

Double{

didSet{

level = Int(maxSpeed / 10) + 1

}

}

String {

super.description() + " level is \(level)"

}

}

var aBicycle = SpeedLevel()

aBicycle.maxSpeed = 40

println(aBicycle.description())

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

猜你在找的Swift相关文章