swift 4.0 协议 协议扩展

协议


import UIKit


protocol Breathable {
    var isBreathing:Bool {get set}
    var isLiving:Bool {get}
}

struct human:Breathable {
//    var isLiving: Bool = true
//    var isBreathing: Bool = true
    
    
    var isBreathing: Bool {
        get{return true}
        set{}
    }
    
    var isLiving: Bool {
        get{return true}
        set{}
    }
}

protocol superman :People {
    var canFly: Bool {get set}
    func punch()
}


struct AngleLisa:superman {
    var name: String = "Lisa"
    var race: String = "Asian"
    func sayHi() {
        print("hi~ i'm\(name)")

    }
    var canFly: Bool = true
    func punch() {
        print("哈哈哈")
    }
}

protocol People {
    
    var name:String {get set}
    var race:String {get set}
    func sayHi()
}
//一旦遵循了协议 就要实现里面的方法和变量
struct Lisa:People {
    var name:String = "lisa"
    var race: String = "Asian"
    func sayHi() {
        print("hi~ i'm\(name)")
    }
}

class lili: People {
    var name:String = "lili"
    var race: String = "Asian"
    func sayHi() {
        print("hi~ i'm\(name)")
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
}

协议扩展

import UIKit

protocol iOSGenius {
    func point()
}

struct iOSLisa : iOSGenius {
    func point() {
        print("lisaa")
    }
}
class iOSDev: iOSGenius {
    
}
extension iOSGenius {
    func point() {
         print("lisa iss   ")
    }
}
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       let dev = iOSDev()
        dev.point()
        
    }

}

你可能感兴趣的:(swift 4.0 协议 协议扩展)