14-协议

2023-04-11  本文已影响0人  二斤寂寞

\color{green}{协议(Protocol)}

protocol Drawable {
  func draw()
     var x: Int { get set }
     var y: Int { get }
     subscript(index: Int) -> Int { get set }                                             
} 
protocol Test1 {}
protocol Test2 {}
protocol Test3 {}
class TestClass : Test1, Test2, Test3 {}

\color{green}{协议中的属性}

protocol Drawable {
    func draw()                                           
    var x: Int { get set }
    var y: Int { get }
    subscript(index: Int) -> Int { get set }                                               
} 
class Person : Drawable {
    var x: Int = 0
    let y: Int = 0
    func draw() {
        print("Person draw")
    }                                            
    subscript(index: Int) -> Int { 
        set {}                                              
        get { index } 
    }                                              
}
class Person : Drawable {
    var x: Int {                                       
        get { 0 }                                          
        set {} 
    }                                              
    var y: Int { 0 }
    func draw() { print("Person draw") } 
    subscript(index: Int) -> Int { 
        set {}
        get { index }
    }
}

\color{green}{static} \color{green}{class}

protocol Drawable {
  static func draw()                                      
} 

class Person1 : Drawable {
  class func draw() {
     print("Person1 draw")
  }                                             
} 

class Person2 : Drawable {
   static func draw() {
     print("Person2 draw")
   }                                              
} 

\color{green}{mutating}

protocol Drawable {
     mutating func draw()
} 

class Size : Drawable {
     var width: Int = 0
     func draw() {                                        
        width = 10 
     }                                             
} 

struct Point : Drawable {
     var x: Int = 0
     mutating func draw() {
         x = 10 
     } 
} 

\color{green}{init}

protocol Drawable {
   init(x: Int, y: Int)    
} 

class Point : Drawable { 
   required init(x: Int, y: Int) {} }} 
}

final class Size : Drawable {
    init(x: Int, y: Int) {}                                              
} 

protocol Livable {
   init(age: Int)                                              
}  

class Person {                                         
    init(age: Int) {}
} 

class Student : Person, Livable {
    required override init(age: Int) {
       super.init(age: age)            
    }
}

\color{green}{init} \color{green}{init?} \color{green}{init!}

protocol Livable {
    init()
    init?(age: Int)
    init!(no: Int)
}

class Person : Livable {
    required init() {}
    // required init!() {}                                      

    required init?(age: Int) {}
    // required init!(age: Int) {}
    // required init(age: Int) {}

    required init!(no: Int) {}
    // required init?(no: Int) {}
    // required init(no: Int) {}                       
} 

\color{green}{协议的继承}

protocol Runnable {
   func run()                                                 
} 

protocol Livable : Runnable {
   func breath()                                             
} 

class Person : Livable {
   func breath() {}
   func run() {}                                             
} 

\color{green}{协议组合}

protocol Livable {}
protocol Runnable {}
class Person {}
// 接收Person或者其子类的实例
 func fn0(obj: Person) {}
 // 接收遵守Livable协议的实例
 func fn1(obj: Livable) {}
 // 接收同时遵守Livable、Runnable协议的实例
 func fn2(obj: Livable & Runnable) {}
 // 接收同时遵守Livable、Runnable协议、并且是Person或者其子类的实例 
 func fn3(obj: Person & Livable & Runnable) {} 
typealias RealPerson = Person & Livable & Runnable
 // 接收同时遵守Livable、Runnable协议、并且是Person或者其子类的实例 
func fn4(obj: RealPerson) {} 

\color{green}{CaseIterable}

enum Season : CaseIterable {
    case spring, summer, autumn, winter
}

let seasons = Season.allCases
print(seasons.count) // 4
for season in seasons {
    print(season)
} // spring summer autumn winter

\color{green}{CustomStringConvertible}

class Person : CustomStringConvertible, CustomDebugStringConvertible { 
    var age = 0                                                  
    var description: String { "person_\(age)" }                                              
    var debugDescription: String { "debug_person_\(age)" } 
}                                              
var person = Person()
print(person) // person_0 
debugPrint(person) // debug_person_0 
image.png

\color{green}{Any} \color{green}{AnyObject}

var stu: Any = 10
stu = "Jack"
stu = Student()
// 创建1个能存放任意类型的数组 
// var data = Array<Any>() 
var data = [Any]() 
data.append(1) 
data.append(3.14) 
data.append(Student()) 
data.append("Jack") 
data.append({ 10 }) 

\color{green}{is} \color{green}{as?} \color{green}{as!} \color{green}{as}

protocol Runnable { func run() } 
class Person {}
class Student : Person, Runnable {                                                  
    func run() {
        print("Student run")
    }
    func study() {
        print("Student study")
    }                                            
} 
var stu: Any = 10
print(stu is Int) // true
stu = "Jack"
print(stu is String) // true 
stu = Student()
print(stu is Person) // true 
print(stu is Student) // true 
print(stu is Runnable) // true 
var stu: Any = 10
(stu as? Student)?.study() // 没有调用study 
stu = Student()
(stu as? Student)?.study() // Student study 
(stu as! Student).study() // Student study 
(stu as? Runnable)?.run() // Student run 
var data = [Any]() 
data.append(Int("123") as Any) 

var d = 10 as Double
print(d) // 10.0   

\color{green}{X.self} \color{green}{X.Type} \color{green}{AnyClass}

class Person {}
class Student : Person {}
var perType: Person.Type = Person.self 
var stuType: Student.Type = Student.self 
perType = Student.self 
var anyType: AnyObject.Type = Person.self 
anyType = Student.self 

public typealias AnyClass = AnyObject.Type 
var anyType2: AnyClass = Person.self 
anyType2 = Student.self 
var per = Person()
var perType = type(of: per) // Person.self 
print(Person.self == type(of: per)) // true 

\color{green}{元类型的应用}

class Animal { required init() {} } 
class Cat : Animal {}
class Dog : Animal {}
class Pig : Animal {} 

func create(_ clses: [Animal.Type]) -> [Animal] { 
    var arr = [Animal]()
    for cls in clses {                                              
        arr.append(cls.init()) 
    }                                              
    return arr 
} 

print(create([Cat.self, Dog.self, Pig.self])) 

\color{green}{元类型的应用}

import Foundation
class Person {
    var age: Int = 0
}

class Student : Person {
   var no: Int = 0                                              
}

 print(class_getInstanceSize(Student.self)) // 32 
 print(class_getSuperclass(Student.self)!) // Person 
 print(class_getSuperclass(Person.self)!) // Swift._SwiftObject 

\color{green}{Self

class Person {
   var age = 1
   static var count = 2
   func run() {
      print(self.age) // 1
      print(Self.count) // 2
    }                                                 
} 
protocol Runnable {
    func test() -> Self
}

class Person : Runnable {
    required init() {}                                           
    func test() -> Self { type(of: self).init() } 
} 

class Student : Person {}

var p = Person()
// Person
print(p.test())

var stu = Student()
// Student
print(stu.test())
上一篇 下一篇

猜你喜欢

热点阅读