網站首頁 編程語言 正文
協議(Protocol)
1、協議可以用來定義方法、屬性、下標的聲明,協議可以被枚舉、結構體、類遵守(多個協議之間用逗號隔開)
protocol Drawable {
func draw()
var x: Int { get set }
var y: Int { get }
subscript(index: Int) -> Int { get set }
}
2、協議中定義方法時不能有默認參數值
3、默認情況下,協議中定義的內容必須全部都實現
協議中的屬性
1、協議中定義屬性時必須用var關鍵字
2、實現協議時屬性權限要不小于協議中定義的屬性權限
協議定義get、set,用var存儲屬性或get、set計算屬性去實現
協議定義get,用任何屬性都可以實現
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 {
get { 0 }
}
func draw() {
print("Person draw")
}
subscript(index: Int) -> Int {
set {}
get { index }
}
}
static、class
1、為了保證通用,協議中必須用static定義類型方法、類型屬性、類型下標
protocol Drawable {
static func draw()
}
class Person1: Drawable {
class func draw() {
print("Person1 draw")
}
}
class Person2: Drawable {
static func draw() {
print("Person2 draw")
}
}
mutating
1、只有將協議中的實例方法標記為mutating
才允許結構體、枚舉的具體實現修改自身內存
類在實現方法時不用加mutating,枚舉、結構體才需要加mutating
protocol Drawable {
mutating func draw()
}
class Size: Drawable {
var width: Int = 0
func draw() {
width = 10
}
}
static Point: Drawable {
var x: Int = 0
mutating func draw() {
x = 10
}
}
init
1、協議里面還可以定義初始化器init
非final類實現時必須加上required
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) {
}
}
2、如果從協議實現的初始化器,剛好是重寫了父類的指定初始化器
那么這個初始化必須同時加required、override
protocol Liveable {
init(age: Int)
}
class Person {
init(age: Int) {}
}
class Student: Person, Liveable {
required override init(age: Int) {
super.init(age: age)
}
}
init、init?、init!
1、協議中定義的init?、init!,可以用init、init?、init!去實現
2、協議中定義的init,可以用init、init!去實現
protocol Liveable {
init()
init?(age: Int)
init!(no: Int)
}
class Person: Liveable {
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) {}
}
協議的繼承
1、一個協議可以繼承其他協議
協議組合
1、協議組合,可以包含1個類類型(最多1個)
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
func fn4(obj: RealPerson) {}
CaseIterable
1、讓枚舉遵守CaseIterable協議,可以實現遍歷枚舉值
enum Season: CaseIterable {
case spring, summer, autumn, winter
}
let seasons = Season.allCases
print(seasons.count) // 4
for season in seasons {
print(season)
}
CustomStringConvertible
1、遵守CustomStringConvertible協議,可以自定義實例的打印字符串
class Person: CustomStringConvertible {
var age: Int
var name: String
init(age: Int, name: String) {
self.age = age
self.name = name
}
var description: String {
"age = \(age), name = \(name)"
}
}
var p = Person(age: 10, name: "Jack")
print(p) // age = 10, name = Jack
原文鏈接:https://blog.csdn.net/run_in_road/article/details/125980604
相關推薦
- 2023-04-01 路由react-router-dom的基本使用教程_React
- 2022-09-20 go語言VScode?see?'go?help?modules'?(exit?status?1)問題
- 2022-05-10 @requestmapping獲取請求參數
- 2023-07-16 spring boot多模塊打包 運行
- 2022-11-15 python列表中常見的一些排序方法_python
- 2022-05-20 Docker容器鏡像加載及底層基本原理深入解析_docker
- 2022-08-12 Pandas中DataFrame常用操作指南_python
- 2022-06-16 VS?Code?C++環境的搭建過程_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支