zoukankan      html  css  js  c++  java
  • swift 继承相关

    class Vehicle {

        var numberOfWheels: Int

        var maxPassengers: Int

        

        func description() -> String {

            return "(numberOfWheels) wheels; up to (maxPassengers) passengers"

        }

        

        init() {

            numberOfWheels = 1;

            maxPassengers = 3;

        }

    }

     

    class Bicycle: Vehicle {

        init() {

            super.init();

            numberOfWheels = 2;

        }

    }

     

     

    class Tandem: Bicycle {

        init() {

            super.init();

            maxPassengers = 2;

        }

        

    }

     

    //override func

    class Car : Vehicle {

        var speed : Double = 0.0;

        init() {

            super.init()

            maxPassengers = 5

            numberOfWheels = 4;

        }

        override func description() -> String {

            return super.description() + ";" + "speed at (speed) mph";

        }

    }

     

    //override properties

    class SpeedLimit : Car {

        override var speed: Double {

        get {

            return super.speed

        }

        set {

            super.speed = max(newValue, 40.0)

        }

        }

    }

     

    let limit = SpeedLimit();

    limit.speed = 220.0

     

    println("LimitSpeed:(limit.description())")

     

    //重写属性观察器

    class AutomaticCar: Car {

        var gear = 1;

        override var speed: Double {

        didSet {

            gear = Int(speed/10) + 1

        }

        }

        override func description() -> String {

            return super.description() + " in gead at (gear)";

        }

        

    }

    let automaticCar = AutomaticCar()

    automaticCar.speed = 50.0

    println(automaticCar.description())

  • 相关阅读:
    idea从零搭建简单的springboot+Mybatis
    关于监听微服务功能
    final
    mysql数据库分页查询优化
    Mysql中“select ... for update”排他锁(转)
    HashMap(常用)方法个人理解
    5.1 Object类型
    5.3 Date类型
    4.1-4.2 基本类型及引用类型,执行环境及作用域
    20.JSON
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3790622.html
Copyright © 2011-2022 走看看