国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Swift 常用標準庫協議

2019-11-08 00:04:33
字體:
來源:轉載
供稿:網友

struct Size: Equatable, Comparable, CustomStringConvertible { // 相等的協議    var width: Double    var height: Double        var description: String {        return "witth:" + "/(self.width)" + " " + "height" + "/(self.height)"    }}// 相等協議的實現func ==(left: Size, right: Size) -> Bool {    return left.height == right.height && left.width == right.height}// 比較協議的實現func <(left: Size, right: Size) ->Bool {    if left.width * left.height != right.height * right.width {        return left.width * left.height < right.height * right.width    }        return left.width * left.height > right.height * right.width}

擴展協議

PRotocol student:CustomStringConvertible {    var name: String{get}    var height: Double {get}    }// 擴展協議,在協議擴展中可以添加具體的邏輯extension student {    var description: String {        return "name is" + " /(name)" + " " + "height is" + " /(height)"    }        func showInfo() {        print("good student")    }}// 擴展協議 CustomStringConvertibleextension CustomStringConvertible {    var descriptionWithDate:String {        return NSDate().description + " " + description    }}class Person: student {    var name: String = ""    var height: Double = 0.0        init(name: String, height: Double) {        self.name = name        self.height = height    }}var person = Person(name: "jobs", height: 1.80)print(person)person.showInfo()print(person.descriptionWithDate)

上面代碼打印結果:

name is jobs height is 1.8

good student

2017-02-24 08:57:15 +0000 name is jobs height is 1.8

對已有的協議修改

protocol Student: CustomStringConvertible {    var name: String{get}    var height: Double {get}    }// 擴展協議,在協議擴展中可以添加具體的邏輯extension Student {    var description: String {        return "name is" + " /(name)" + " " + "height is" + " /(height)"    }        func showInfo() {        print("good student")    }}protocol HighSchoolStudent {    var age: Int {get}}// 對已有的協議做修改 當類準守HighSchoolStudent的協議時 打印 good HighSchoolStudentextension Student where Self:HighSchoolStudent {    func showInfo() {        print("name is" + " /(name)" + " " + "height is" + " /(height)" + " " + "age is " + "/(age)")    }}// 擴展協議 CustomStringConvertibleextension CustomStringConvertible {    var descriptionWithDate:String {        return NSDate().description + " " + description    }}class Person: Student {    var name: String = ""    var height: Double = 0.0        init(name: String, height: Double) {        self.name = name        self.height = height    }}class People: Student, HighSchoolStudent {    internal var height: Double = 0.0    internal var name: String = ""    internal var age: Int = 0    init(height:Double, name: String, age: Int) {        self.name = name        self.age = age        self.height = height    }}var person = Person(name: "jobs", height: 1.80)print(person)person.showInfo()print(person.descriptionWithDate)var people = People(height: 1.7, name: "zhangsan", age: 17)people.showInfo()上面代碼打印結果:

name is jobs height is 1.8

good student

2017-02-24 09:44:12 +0000 name is jobs height is 1.8

name is zhangsan height is 1.7 age is 17


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳春市| 恩施市| 会同县| 斗六市| 钦州市| 卢氏县| 云龙县| 宜兴市| 临江市| 乌苏市| 太湖县| 旬阳县| 双鸭山市| 汉中市| 松潘县| 固阳县| 尚志市| 京山县| 乐昌市| 荃湾区| 南涧| 花垣县| 浑源县| 改则县| 石阡县| 新邵县| 甘孜| 通河县| 广州市| 锦屏县| 岑巩县| 定日县| 庆阳市| 黔西| 壤塘县| 卢湾区| 盐边县| 长海县| 甘洛县| 布尔津县| 咸丰县|