Skip to content

Commit ca16565

Browse files
authored
Merge pull request SwiftGGTeam#655 from chenmingbiao/gh-pages
update chapter Protocols to swift 3.0
2 parents df86057 + 17757b4 commit ca16565

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

source/chapter2/22_Protocols.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
1111
> 2.1
1212
> 翻译:[小铁匠Linus](https://github.com/kevin833752)
13-
> 校对:[shanks](http://codebuild.me),2015-11-01
13+
> 校对:[shanks](http://codebuild.me)
1414
>
1515
> 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)
1720
1821
本页包含内容:
1922

@@ -614,7 +617,7 @@ protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
614617
<a name="protocol_composition"></a>
615618
## 协议合成
616619

617-
有时候需要同时采纳多个协议,你可以将多个协议采用 `protocol<SomeProtocol, AnotherProtocol>` 这样的格式进行组合,称为 *协议合成(protocol composition)*你可以在 `<>` 中罗列任意多个你想要采纳的协议,以逗号分隔
620+
有时候需要同时采纳多个协议,你可以将多个协议采用 `SomeProtocol & AnotherProtocol` 这样的格式进行组合,称为 *协议合成(protocol composition)*你可以罗列任意多个你想要采纳的协议,以与符号(`&`)分隔
618621

619622
下面的例子中,将 `Named``Aged` 两个协议按照上述语法组合成一个协议,作为函数参数的类型:
620623

@@ -629,17 +632,17 @@ struct Person: Named, Aged {
629632
var name: String
630633
var age: Int
631634
}
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)!")
634637
}
635638
let birthdayPerson = Person(name: "Malcolm", age: 21)
636-
wishHappyBirthday(birthdayPerson)
639+
wishHappyBirthday(to: birthdayPerson)
637640
// 打印 “Happy birthday Malcolm - you're 21!”
638641
```
639642

640643
`Named` 协议包含 `String` 类型的 `name` 属性。`Aged` 协议包含 `Int` 类型的 `age` 属性。`Person` 结构体采纳了这两个协议。
641644

642-
`wishHappyBirthday(_:)` 函数的参数 `celebrator` 的类型为 `protocol<NamedAged>`。这意味着它不关心参数的具体类型,只要参数符合这两个协议即可。
645+
`wishHappyBirthday(_:)` 函数的参数 `celebrator` 的类型为 `Named & Aged`。这意味着它不关心参数的具体类型,只要参数符合这两个协议即可。
643646

644647
上面的例子创建了一个名为 `birthdayPerson``Person` 的实例,作为参数传递给了 `wishHappyBirthday(_:)` 函数。因为 `Person` 同时符合这两个协议,所以这个参数合法,函数将打印生日问候语。
645648

0 commit comments

Comments
 (0)