Swift 系统学习 30 枚举 结构体 类 遵循协议 protocol

//: Playground - noun: a place where people can play

import UIKit

/*
 * 本节主要内容:
 * 1.枚举 / 结构体 / 类遵循协议
 */

protocol Togglable {
    mutating func toggle() // 没有大括号
}

// OnOffSwitch枚举类型遵循了Togglable协议
enum OnOffSwitch: Togglable {
    case Off
    case On
    
    mutating func toggle() {
        switch self {
        case .Off:
            self = .On
        case .On:
            self = .Off
        }
    }
}
var switchOne = OnOffSwitch.On

var array: Array = []
protocol FullNamed {
    // 属性: var + 类型 + get / get set
    var fullName: String { get } // { get set }
    // 自定义方法
    func randomValue() -> Int
    // 类型方法(和类型属性对应)
    static func someTypeMethod()
    // mutating关键词的方法
    mutating func changeName(newName: String)
}
struct Person: FullNamed {
    // ***如果协议的属性是只读, 结构体遵循协议, 属性没有任何要求
     // 存储属性
    var fullName: String {
        get {
            return ""
        }
        set {
            
        }
    }
    var firstName: String?
    func randomValue() -> Int {
        return Int(arc4random()%50)
    }
    static func someTypeMethod() {
         print("Call the someTypeMethod.")
    }
    mutating func changeName(newName: String) {
        fullName = newName
    }
}
Person.someTypeMethod()


protocol Pet {
    var name: String { get set }
    var birthPlace: String { get }
    func playWith()
    func fedWith(food: String)
    func changeName(newName: String)
}

// class Dog: SuperClass, ProtocolName
class Dog: Pet {
    private var myDoggyName = "Puppy"
    var name: String {
        get { return myDoggyName  }
        set { myDoggyName = newValue }
    }
    
    var birthPlace: String = "Beijing"
    func playWith() {
        print("Play with it for a while.")
    }
    func fedWith(food: String = "Bone") {
        if food == "Bone" {
            print("Happy")
        } else {
            print("Unhapy")
        }
    }
    func changeName(newName: String) {
        name = newName
    }
}


/*
 * 思考问题: 假设没有声明Pet宠物协议; 声明Pet类, 让dog类继承于Pet
 */





import UIKit

/*
 * 本节内容: 实现笔记中的流程图
 */

protocol Pet {
    var name: String { get set }
}

protocol Flyable {
    var flySpeed: Double { get }
    var flyHeight: Double { get }
}

class Animal {
    
}
class Dog: Animal, Pet {
    var name: String = "Puppy"
}
class Cat: Animal, Pet {
    var name: String = "Kitty"
}
class Bird: Animal, Flyable {
    var flySpeed: Double
    var flyHeight: Double
    init(flySpeed: Double, flyHeight: Double) {
        self.flySpeed  = flySpeed
        self.flyHeight = flyHeight
    }
}
class Parrot: Bird, Pet {
    var name: String
    init(name: String, flySpeed: Double, flyHeight: Double) {
        self.name = name
        super.init(flySpeed: flySpeed, flyHeight: flyHeight)
    }
}
class Sparrow: Bird {
    var color = UIColor.gray
}

class Vehicle {
    
}
class Plane: Vehicle, Flyable {
    var flySpeed: Double
    var flyHeight: Double
    init(flySpeed: Double, flyHeight: Double) {
        self.flySpeed  = flySpeed
        self.flyHeight = flyHeight
    }
}









你可能感兴趣的:(Swift,系统学习系列)