Swift 學習筆記 協議

2021-08-07 13:57:34 字數 3347 閱讀 6761

如果類含有父類,則應當將父類放在所有的協議之前。

// 協議

protocol oneprotocol或者

var argumentone:int // 唯讀

var argumenttwo:int // 讀寫

static

var argumentclass:int

}// 類

class

person:oneprotocol

}init(argumentone:int,argumenttwo:int)

}var per = person(argumentone: 90, argumenttwo: 1)

print("\(per.argumentone)") // 90

print("\(person.argumentclass)") // 30

protocol funcprotocol

class person:funcprotocol

static func saybad()

}

protocol togglable

enum onoffswitch:togglable

}}var light = onoffswitch.off //off

light.toggle() // on

struct onoff:togglableelse

}}var light = onoff(one: 2, two: 3)

print("\(light.two)") // 3

light.toggle()

print("\(light.two)") // 20

protocol

twoprotocol

class two:twoprotocol

}

protocol twoprotocol

class

twosuperclass

}class

two:twosuperclass,twoprotocol

}

protocol twoprotocol

class

twosuperclass

}class

two:twosuperclass,twoprotocol

protocol textprotocol

class dice

extension dice:textprotocol

}let dic = dice()

dic.printsomemessage(message: "hello");

protocol textprotocol

struct hamster

}extension hamster:textprotocol

let hamster = hamster()

let things:[textprotocol] = [dic,hamster]

for thing in things

// 由於thing被當作是textprotocol型別,故都能呼叫printsomemessage方法

protocol someonlyclassprotocol:class

// 協議someonlyclassprotocol實現者只能是類

protocol oneprotocol

// 協議的繼承

protocol twoprotocol:oneprotocol

class person:twoprotocol

func saytwo()

}

protocol namedprotocol

}protocol genderprotocol

}protocol agedprotocol

}struct person:namedprotocol,genderprotocol,agedprotocol

func wishbirthday(celebrator:namedprotocol & genderprotocol)

var per = person(name: "wang", gender: "man",age:20)

wishbirthday(celebrator: per)

// 形參celebrator的型別為protocol,可以傳入任何實現這兩個協議的例項。即使此例項不僅僅遵循這兩個協議。

// swift3.0之後,protocol被namedprotocol & genderprotocol替換。

@objc protocol hasarea

}class circle:hasarea

init(radius:double)

}class country:hasarea

}class animal

}let objects:[anyobject] = [

circle(radius: 20),

country(area: 200),

animal(legs: 10)

]for

object

in objects else

}print("--------------------")

forobject

in objects else

}/**

列印結果:

optional(__lldb_expr_386.circle)

遵循了hasarea協議,optional(1256.63708)

optional(__lldb_expr_386.country)

遵循了hasarea協議,optional(200.0)

沒有遵循hasarea協議

--------------------

true

遵循了hasarea協議,optional(1256.63708)

true

遵循了hasarea協議,optional(200.0)

沒有遵循hasarea協議

*/

@objc protocol hasarea

var width:double

}class

circle:hasarea

Swift學習筆記 協議擴充套件

在swift中進行物件導向程式設計時,盡量使用協議和泛型,而不是類繼承,因為過深的繼承層次很容易讓 充滿難以理解的類。前面我們介紹過了類的擴充套件,協議的擴充套件也是類似的,協議擴充套件可以新增現有的計算屬性和方法,不能新增儲存屬性。擴充套件不會增加協議的需求。下面的 中,所有實現worker協議的...

Swift學習筆記十六 協議

protocol 協議 用於統一方法和屬性的名稱,而不實現不論什麼功能。協議可以被類。列舉。結構體實現。滿足協議要求的類,列舉,結構體被稱為協議的 遵循者。遵循者須要提供 協議指定的成員,如屬性,方法,操作符,下標等。一 協議的基本的語法 咱們還是先上 吧 protocol human var is...

Python學習筆記 協程

二十 協程 協程,又稱微執行緒,纖程,coroutine 子程式 又稱為函式 在所有語言中都是層級呼叫 a呼叫b b呼叫c c返回b b 返回 a 所以子程式呼叫是通過棧實現的 乙個執行緒就是執行乙個子程式 子程式呼叫總是乙個入口 一次返回 呼叫順序是明確的 而協程的呼叫和子程式不同 協程看上去也是...