GeekBand-Swift 第二周笔记(结构、枚举、类)

2017-03-01  本文已影响0人  周一见丶

结构、枚举、类

继承、多态、协议

字符串和集合

String、Array、Set、Dictionary 都是 Struct,其中 String 内存模型稍稍复杂一点,其他三位都是栈中储存一个指针指向真正的对象,那为什么说他们是值类型 Struct 呢?因为他们都是值语义。


String 内存模型.png

第二周作业

第二周作业题目.png

我的答案

import UIKit
import Foundation
//交通工具类
class Jiaotong {
    var v: Double
    var w: Double
    init(v: Double, w: Double) {
        self.v = v
        self.w = w
    }
    func move(time: Double, v: Double) {
        let l = time*v
        print("移动距离\(l)")
    }
    deinit {
        print("Over")
    }
}
//颜色枚举
enum Color {
    case white
    case blue
    case yellow
    case black
    case green
    case red
}
//火车类继承交通工具
class Train: Jiaotong {
    var color: Color
    var chexiang = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"]
    init(color: Color) {
        self.color = color
        super.init(v: 80.0, w: 800.0)
    }
    //便捷初始化器
    convenience init() {
        self.init(color: .white)
    }
    //定义下标
    subscript (i: Int) -> String {
        get {
            return chexiang[i]
        }
        set {
            chexiang[i] = newValue
        }
    }
    //重写父类移动方法
    override func move(time: Double, v: Double) {
        super.move(time: time, v: v)
        print("嗡嗡")
    }
}
//飞机的机长结构
struct Captain {
    let name: String
    var worktime: Double
    init(name: String, worktime: Double) {
        self.name = name
        self.worktime = worktime
        print("The aircraft's captain is \(self.name), he worktime is \(self.worktime) hours.")
    }
    mutating func addWorktime(newWorktime: Double) {
        self.worktime += newWorktime
        print("The aircraft's captain is \(self.name), he worktime is \(self.worktime) hours.")
    }
}
//飞机类继承交通工具
class Aircraft: Jiaotong {
    var color: Color
    var captain: Captain
    //使用可选类型定义初始化器
    init?(color: Color, captain: Captain) {
        //当机长姓名为空时,初始化失败返回 nil
        if captain.name.isEmpty {
            return nil
        }
        self.captain = captain
        self.color = color
        super.init(v: 800.0, w: 50.0)
    }
//自定义操作符比较这两个类的颜色
    static func == (left: Train, right: Aircraft) -> Bool {
        if left.color == right.color {
            return true
        }
            return false
    }
    override func move(time: Double, v: Double) {
        super.move(time: time, v: v)
        print("呼呼")
    }
}
//新建各个类的实例,实现里面的方法
var jiaotong = Jiaotong(v: 0.0, w: 0.0)
var train1 = Train()
var train2 = Train(color: .green)
var captain1 = Captain(name: "Davie", worktime: 1055.5)
var aircraft1 = Aircraft(color: .white, captain: captain1)

jiaotong.move(time: 10, v: 10.5)
train1.move(time: 50.5, v: 135.6)
aircraft1?.move(time: 34.5, v: 800.0)
aircraft1?.captain.addWorktime(newWorktime: 10.5)

//用下标遍历车厢元素
for i in 0...9 {
    print(train1[i])
}
//先判断飞机实例是否为空,再比较火车和飞机的实例颜色是否相同
if let aircraftLet = aircraft1{
    if train1 == aircraftLet {
        print("The train1's color is the same as the aircraft.")
    }
    else {
        print("The train1's color is not the same as the aircraft.")
    }
}
else {
    print("The aircraft has not been initialized yet.")
}
//可选类型赋值为 nil 可以验证析构器
aircraft1 = nil
上一篇下一篇

猜你喜欢

热点阅读