Clarence

针对有一定Objective-C基础或者其他语言基础的人群(实际上耐心看边看边敲代码,零基础也是可以看懂的)...

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<Int> = []
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
    }
}









阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。本文针对于有一定Objective-C语言基础的人群,听说面试的时候要博客,然后就整理了一点资料发出来, 共同学习!! https://blog.csdn.net/clarence20170301/article/details/59108996
个人分类: Swift 系统学习系列
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭