10
10
11
11
> 2.1
12
12
> 翻译:[ 小铁匠Linus] ( https://github.com/kevin833752 )
13
- > 校对:[ shanks] ( http://codebuild.me ) ,2015-11-01
13
+ > 校对:[ shanks] ( http://codebuild.me )
14
14
>
15
15
> 2.2
16
- > 翻译+校对:[ SketchK] ( https://github.com/SketchK ) 2016-05-16
16
+ > 翻译+校对:[ SketchK] ( https://github.com/SketchK )
17
+ >
18
+ > 3.0
19
+ > 校对:[ CMB] ( https://github.com/chenmingbiao )
17
20
18
21
本页包含内容:
19
22
@@ -614,7 +617,7 @@ protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
614
617
<a name =" protocol_composition " ></a >
615
618
## 协议合成
616
619
617
- 有时候需要同时采纳多个协议,你可以将多个协议采用 ` protocol< SomeProtocol, AnotherProtocol> ` 这样的格式进行组合,称为 * 协议合成(protocol composition)* 。你可以在 ` <> ` 中罗列任意多个你想要采纳的协议,以逗号分隔 。
620
+ 有时候需要同时采纳多个协议,你可以将多个协议采用 ` SomeProtocol & AnotherProtocol ` 这样的格式进行组合,称为 * 协议合成(protocol composition)* 。你可以罗列任意多个你想要采纳的协议,以与符号( ` & ` )分隔 。
618
621
619
622
下面的例子中,将 ` Named ` 和 ` Aged ` 两个协议按照上述语法组合成一个协议,作为函数参数的类型:
620
623
@@ -629,17 +632,17 @@ struct Person: Named, Aged {
629
632
var name: String
630
633
var age: Int
631
634
}
632
- func wishHappyBirthday (celebrator : protocol < Named, Aged> ) {
633
- print (" Happy birthday \( celebrator.name ) - you're \( celebrator.age ) !" )
635
+ func wishHappyBirthday (to celebrator : Named & Aged) {
636
+ print (" Happy birthday, \( celebrator.name ) , you're \( celebrator.age ) !" )
634
637
}
635
638
let birthdayPerson = Person (name : " Malcolm" , age : 21 )
636
- wishHappyBirthday (birthdayPerson)
639
+ wishHappyBirthday (to : birthdayPerson)
637
640
// 打印 “Happy birthday Malcolm - you're 21!”
638
641
```
639
642
640
643
` Named ` 协议包含 ` String ` 类型的 ` name ` 属性。` Aged ` 协议包含 ` Int ` 类型的 ` age ` 属性。` Person ` 结构体采纳了这两个协议。
641
644
642
- ` wishHappyBirthday(_:) ` 函数的参数 ` celebrator ` 的类型为 ` protocol< Named, Aged> ` 。这意味着它不关心参数的具体类型,只要参数符合这两个协议即可。
645
+ ` wishHappyBirthday(_:) ` 函数的参数 ` celebrator ` 的类型为 ` Named & Aged ` 。这意味着它不关心参数的具体类型,只要参数符合这两个协议即可。
643
646
644
647
上面的例子创建了一个名为 ` birthdayPerson ` 的 ` Person ` 的实例,作为参数传递给了 ` wishHappyBirthday(_:) ` 函数。因为 ` Person ` 同时符合这两个协议,所以这个参数合法,函数将打印生日问候语。
645
648
0 commit comments