协议的语法
- protocol SomeProtocol {
- //定义了一个协议
- }
- protocol AnotherProtocol {
- //定义了一个协议
- }
- protocol OneProtocol: SomeProtocol, AnotherProtocol {
- //定义了一个协议 OneProtocol, 他分别遵循 SomeProtocol 和 AnotherProtocol 协议
- }
- class SomeSuperClass {
- //定义了一个基类
- }
- class SomeClas: SomeSuperClass, SomeProtocol, AnotherProtocol {
- //定义了一个类 SomeClas, 他有一个父类 SomeSuperClass, 并且他遵循了 SomeProtocol 和 AnotherProtocol 协议
- }
- protocol SomeProtocol {
- var mustBeSettable: Int { get set } //协议要求属性必须是可写的
- var doesNotNeedTobeSettable: Int { get } //协议要求属性可以不是可写的
- }
-
- protocol FullyNamed {
- var fullName: String { get }
- }
- struct Person: FullyNamed {
- var fullName: String
- }
- let john = Person(fullName: "John")
- class StarShip: FullyNamed {
- var prefix: String?
- var name: String
- init(name: String, prefix: String? = nil) {
- self.name = name
- self.prefix = prefix
- }
- var fullName: String { // StarShip 类遵循了 FullyNamed 协议,必须要实现协议中属性的可读属性
- return (prefix != nil ? prefix! + " " : "") + name
- }
- }
- var sta = StarShip(name: "EnterPrice", prefix: "USS")
- print(sta.name)
- print(sta.fullName)
- print(sta.prefix ?? "")
- protocol SomeProtocol {
- //在协议中定义类型属性时在前面添加 static 关键字
- static var someTypeProperty: Int { get }
- }
- class someClass: SomeProtocol {
- static var someTypeProperty: Int {
- return 3
- }
- }
- //枚举是值类型,在值类型的实例方法中,将mutating关键字作为函数的前缀,写在func之前,表示可以在该方法中修改它所属的实例及其实例属性的值。
- enum Directions: Direction {
- case north, south, east, west
- mutating func show() {
- switch self {
- case .north:
- self = .north
- print("north")
- case .south:
- self = .south
- print("south")
- case .east:
- self = .east
- print("east")
- default:
- self = .west
- print("west")
- }
- }
- }
- var dir = Directions.east
- print(dir)
- protocol SomeProtocol {
-
- }
- class SomeSuperClass {
-
- }
- class someClass: SomeSuperClass, SomeProtocol {
- required override init() {
-
- }
- }
- protocol TcpProtocol {
- init(no1: Int)
-
- func add(count: Int)
- }
- class MainClass {
- var no1: Int //局部变量
- init(no1: Int) {
- self.no1 = no1 //初始化
- }
- }
- class SubClass: MainClass, TcpProtocol {
- func add(count: Int) {
- print("lalallal")
- }
-
- var no2: Int
- init(no1: Int, no2: Int) {
- self.no2 = no2
- super.init(no1: no1)
- }
- // 因为遵循协议,需要加上"required"; 因为继承自父类,需要加上"override"
- required override convenience init(no1: Int) {
- //便利构造函数 先调用本类的指定初始化器
- self.init(no1: no1, no2: 0)
- }
- }
- let res = MainClass(no1: 10)
- let show = SubClass(no1: 20, no2: 30)
- print("res is: \(res.no1)") // 10
- print("res is: \(show.no1)") // 20
- print("res is: \(show.no2)") // 30
- //协议类型
- protocol Generator {
- associatedtype member
- func next() -> member?
- }
- var items = [10, 20, 30].makeIterator()
- while let x = items.next() {
- print(x)
- }
- /** 输出:
- 10
- 20
- 30
- */
-
- for list in [1, 2, 3].map({ i in i * 5 }) {
- print(list)
- }
- /**
- 输出:
- 5
- 10
- 15
- */
- protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
- // 协议定义
- }
-
- //实例
- protocol Classa {
- var no1: Int{ get set }
- func calculate(sum: Int)
- }
- protocol Result {
- func print(target: Classa)
- }
- class Student: Result {
- func print(target: Classa) {
- target.calculate(sum: 1)
- }
- }
- class Classb: Result {
- func print(target: Classa) {
- target.calculate(sum: 5)
- }
- }
- class Student2: Classa {
- var no1: Int = 10
- func calculate(sum: Int) {
- no1 -= sum
- print("学生尝试 \(sum)次通过")
-
- if no1 <= 0 {
- print("学生缺席考试")
- }
- }
- }
-
- class Player {
- var stmark: Result!
- init(stmark: Result!) {
- self.stmark = stmark
- }
- func print(target: Classa) {
- stmark.print(target: target)
- }
- }
- var marks = Player(stmark: Student())
- var marksec = Student2()
-
- marks.print(target: marksec)
- marks.print(target: marksec)
- marks.print(target: marksec)
- marks.stmark = Classb()
- marks.print(target: marksec)
- marks.print(target: marksec)
- marks.print(target: marksec)
-
- /**输出结果:
- 学生尝试 1次通过
- 学生尝试 1次通过
- 学生尝试 1次通过
- 学生尝试 5次通过
- 学生尝试 5次通过
- 学生缺席考试
- 学生尝试 5次通过
- 学生缺席考试
- */
- protocol SomeProtocol {
-
- }
- protocol someClasslOnlyProtocol: AnyObject, SomeProtocol {
- // AnyObject 可以限制协议只能被类类型采纳
- }
-
- protocol tcpProtocol {
- init(no1: Int)
- }
- class MainClass {
- var no1: Int
- init(no1: Int) {
- self.no1 = no1
- }
- }
- class SubClass: MainClass, tcpProtocol {
- var no2: Int
- init(no1: Int, no2: Int) {
- self.no2 = no2
- super.init(no1: no1) //调用父类的指定初始化器
- }
- // 因为遵循协议,需要加上"required"; 因为继承自父类,需要加上"override"
- required override convenience init(no1: Int) {
- self.init(no1: no1, no2: 0) //便捷初始化器需要先调用本类的指定初始化器
- }
- }
- let res = MainClass(no1: 20)
- let show = SubClass(no1: 30, no2: 50)
-
- print("res is: \(res.no1)")
- print("res is: \(show.no1)")
- print("res is: \(show.no2)")
- /**
- res is: 20
- res is: 30
- res is: 50
- */
- //协议组合
- protocol Named {
- var name: String { get }
- }
- protocol Aged {
- var age: Int { get }
- }
- struct Person: Named, Aged {
- var name: String
- var age: Int
- }
- func WishHappyBirthday(to celebrator: Named & Aged) { //使用 & 符号连接多个协议
- print("Happy birthday! \(celebrator.name), you are \(celebrator.age)")
- }
- let person = Person(name: "zhangsan", age: 14)
- WishHappyBirthday(to: person) // 打印: Happy birthday, zhangsan, you are 14