目录
协议(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