From cdb3267dff59f81ad65820d3971483a917a39ef0 Mon Sep 17 00:00:00 2001 From: Sketchk Date: Fri, 13 May 2016 20:00:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E6=AD=A3+=E7=BF=BB=E8=AF=91=20From=20?= =?UTF-8?q?SketchK=20=E4=B8=BB=E8=A6=81=E5=86=85=E5=AE=B9=201=20`=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE`=E7=AB=A0=E8=8A=82=E4=B8=AD=E7=9A=84`=E9=80=92?= =?UTF-8?q?=E5=BD=92=E6=9E=9A=E4=B8=BE`=E4=B8=80=E8=8A=82=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=86=85=E5=AE=B9=E6=9B=B4=E6=96=B0(?= =?UTF-8?q?=E5=8E=9F=E6=96=87=E4=B8=8E=E5=BD=93=E5=89=8D=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E4=B8=8D=E7=9B=B8=E7=AC=A6)=202=20`=E5=B1=9E?= =?UTF-8?q?=E6=80=A7`=E7=AB=A0=E8=8A=82=E4=B8=AD=E7=9A=84`=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=B1=9E=E6=80=A7`=E4=B8=80=E8=8A=82=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=96=87=E5=AD=97=E5=86=85=E5=AE=B9=E7=BA=A0=E6=AD=A3?= =?UTF-8?q?(=E5=8E=9F=E6=96=87=E4=B8=8E=E5=BD=93=E5=89=8D=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E5=86=85=E5=AE=B9=E4=B8=8D=E7=9B=B8=E7=AC=A6)=203=20`?= =?UTF-8?q?=E6=96=B9=E6=B3=95`=E7=AB=A0=E8=8A=82=E4=B8=AD=E7=9A=84`?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E6=96=B9=E6=B3=95`=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=9C=89=E8=AF=AF(++=E7=AC=A6=E5=8F=B7=E5=BA=9F?= =?UTF-8?q?=E5=BC=83)=204=20`=E6=96=B9=E6=B3=95`=E7=AB=A0=E8=8A=82?= =?UTF-8?q?=E4=B8=AD`self=E5=B1=9E=E6=80=A7`=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=9C=89=E8=AF=AF=E4=BD=9C(++=E7=AC=A6=E5=8F=B7=E5=BA=9F?= =?UTF-8?q?=E5=BC=83)=205=20`=E7=BB=A7=E6=89=BF`=E7=AB=A0=E8=8A=82?= =?UTF-8?q?=E4=B8=AD`=E9=98=B2=E6=AD=A2=E9=87=8D=E5=86=99`=E4=B8=80?= =?UTF-8?q?=E8=8A=82=E4=B8=AD=E7=9A=84=E6=96=87=E5=AD=97=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E7=BA=A0=E6=AD=A3(=E5=8E=9F=E6=96=87=E4=B8=8E=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=BF=BB=E8=AF=91=E4=B8=AD=E7=9A=84=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=B8=8D=E7=9B=B8=E7=AC=A6)=206=20=20=E6=A0=A1=E6=AD=A3?= =?UTF-8?q?=E4=BA=86"=E7=B1=BB=E5=92=8C=E7=BB=93=E6=9E=84=E4=BD=93","?= =?UTF-8?q?=E4=B8=8B=E6=A0=87"=E4=B8=A4=E7=AB=A0=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9,=E6=B2=A1=E6=9C=89=E5=8F=91=E7=8E=B0=E6=98=8E?= =?UTF-8?q?=E6=98=BE=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/08_Enumerations.md | 26 +++++++++++--------- source/chapter2/09_Classes_and_Structures.md | 3 +++ source/chapter2/10_Properties.md | 4 +-- source/chapter2/11_Methods.md | 9 ++++--- source/chapter2/12_Subscripts.md | 5 +++- source/chapter2/13_Inheritance.md | 7 ++++-- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/source/chapter2/08_Enumerations.md b/source/chapter2/08_Enumerations.md index 5a4c40f5..7943563e 100755 --- a/source/chapter2/08_Enumerations.md +++ b/source/chapter2/08_Enumerations.md @@ -10,7 +10,10 @@ > 2.1 > 翻译:[Channe](https://github.com/Channe) -> 校对:[shanks](http://codebuild.me) +> 校对:[shanks](http://codebuild.me), + +> 2.2 +> 翻译+校对:[SketchK](https://github.com/SketchK) 2016-05-13 本页内容包含: @@ -293,11 +296,8 @@ if let somePlanet = Planet(rawValue: positionToFind) { ## 递归枚举(Recursive Enumerations) -当各种可能的情况可以被穷举时,非常适合使用枚举进行数据建模,例如可以用枚举来表示用于简单整数运算的操作符。这些操作符让你可以将简单的算术表达式,例如整数`5`,结合为更为复杂的表达式,例如`5 + 4`。 -算术表达式的一个重要特性是,表达式可以嵌套使用。例如,表达式`(5 + 4) * 2`,乘号右边是一个数字,左边则是另一个表达式。因为数据是嵌套的,因而用来存储数据的枚举类型也需要支持这种嵌套——这意味着枚举类型需要支持递归。 - -*递归枚举(recursive enumeration)*是一种枚举类型,它有一个或多个枚举成员使用该枚举类型的实例作为关联值。使用递归枚举时,编译器会插入一个间接层。你可以在枚举成员前加上`indirect`来表示该成员可递归。 +*递归枚举(recursive enumeration)*是一种枚举类型,它有一个或多个枚举成员使用该枚举类型的实例作为关联值。使用递归枚举时,编译器会插入一个间接层。你可以在枚举成员前加上`indirect`来表示该成员可递归。 例如,下面的例子中,枚举类型存储了简单的算术表达式: @@ -319,7 +319,14 @@ indirect enum ArithmeticExpression { } ``` -上面定义的枚举类型可以存储三种算术表达式:纯数字、两个表达式相加、两个表达式相乘。枚举成员`Addition`和`Multiplication`的关联值也是算术表达式——这些关联值使得嵌套表达式成为可能。 +上面定义的枚举类型可以存储三种算术表达式:纯数字、两个表达式相加、两个表达式相乘。枚举成员`Addition`和`Multiplication`的关联值也是算术表达式——这些关联值使得嵌套表达式成为可能。例如,表达式`(5 + 4) * 2`,乘号右边是一个数字,左边则是另一个表达式。因为数据是嵌套的,因而用来存储数据的枚举类型也需要支持这种嵌套——这意味着枚举类型需要支持递归。下面的代码展示了使用`ArithmeticExpression `这个递归枚举创建表达式`(5 + 4) * 2` + +```swift +let five = ArithmeticExpression.Number(5) +let four = ArithmeticExpression.Number(4) +let sum = ArithmeticExpression.Addition(five, four) +let product = ArithmeticExpression.Multiplication(sum, ArithmeticExpression.Number(2)) +``` 要操作具有递归性质的数据结构,使用递归函数是一种直截了当的方式。例如,下面是一个对算术表达式求值的函数: @@ -334,12 +341,7 @@ func evaluate(expression: ArithmeticExpression) -> Int { return evaluate(left) * evaluate(right) } } - -// 计算 (5 + 4) * 2 -let five = ArithmeticExpression.Number(5) -let four = ArithmeticExpression.Number(4) -let sum = ArithmeticExpression.Addition(five, four) -let product = ArithmeticExpression.Multiplication(sum, ArithmeticExpression.Number(2)) + print(evaluate(product)) // 输出 "18" ``` diff --git a/source/chapter2/09_Classes_and_Structures.md b/source/chapter2/09_Classes_and_Structures.md index 563ab2ca..f1dca69e 100755 --- a/source/chapter2/09_Classes_and_Structures.md +++ b/source/chapter2/09_Classes_and_Structures.md @@ -10,6 +10,9 @@ > 2.1 > 校对:[shanks](http://codebuild.me),2015-10-29 +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-13 + 本页包含内容: - [类和结构体对比](#comparing_classes_and_structures) diff --git a/source/chapter2/10_Properties.md b/source/chapter2/10_Properties.md index 60c4104b..ee844243 100755 --- a/source/chapter2/10_Properties.md +++ b/source/chapter2/10_Properties.md @@ -16,7 +16,7 @@ > 2.2 -> 翻译:[saitjr](https://github.com/saitjr),2016-04-11 +> 翻译:[saitjr](https://github.com/saitjr),2016-04-11,[SketchK](https://github.com/SketchK) 2016-05-13 @@ -41,7 +41,7 @@ 可以在定义存储属性的时候指定默认值,请参考[默认构造器](./14_Initialization.html#default_initializers)一节。也可以在构造过程中设置或修改存储属性的值,甚至修改常量存储属性的值,请参考[构造过程中常量属性的修改](./14_Initialization.html#assigning_constant_properties_during_initialization)一节。 -下面的例子定义了一个名为 `FixedLengthRange` 的结构体,它描述了一个用于表示整型范围的常量,在创建后就不能进行修改: +下面的例子定义了一个名为 `FixedLengthRange` 的结构体,该结构体用于描述整数的范围,且这个范围值在被创建后不能被修改. ```swift struct FixedLengthRange { diff --git a/source/chapter2/11_Methods.md b/source/chapter2/11_Methods.md index f2574ad3..437d533c 100755 --- a/source/chapter2/11_Methods.md +++ b/source/chapter2/11_Methods.md @@ -9,7 +9,10 @@ > 翻译+校对:[DianQK](https://github.com/DianQK) > 2.1 -> 翻译:[DianQK](https://github.com/DianQK),[Realank](https://github.com/Realank) 校对:[shanks](http://codebuild.me),2016-01-18 +> 翻译:[DianQK](https://github.com/DianQK),[Realank](https://github.com/Realank) 校对:[shanks](http://codebuild.me),2016-01-18 +> +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-13 本页包含内容: @@ -33,7 +36,7 @@ class Counter { var count = 0 func increment() { - ++count + count += 1 } func incrementBy(amount: Int) { count += amount @@ -112,7 +115,7 @@ counter.incrementBy(5, numberOfTimes: 3) ```swift func increment() { - self.count++ + self.count += 1 } ``` diff --git a/source/chapter2/12_Subscripts.md b/source/chapter2/12_Subscripts.md index 8b2f89f2..8c48dfa6 100755 --- a/source/chapter2/12_Subscripts.md +++ b/source/chapter2/12_Subscripts.md @@ -9,7 +9,10 @@ > 翻译+校对:[shanks](http://codebuild.me) > 2.1 -> 翻译+校对:[shanks](http://codebuild.me),[Realank](https://github.com/Realank) +> 翻译+校对:[shanks](http://codebuild.me),[Realank](https://github.com/Realank) + +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-13 本页包含内容: diff --git a/source/chapter2/13_Inheritance.md b/source/chapter2/13_Inheritance.md index 61b53052..64028c3d 100755 --- a/source/chapter2/13_Inheritance.md +++ b/source/chapter2/13_Inheritance.md @@ -6,7 +6,10 @@ > 校对:[menlongsheng](https://github.com/menlongsheng) > 2.0,2.1 -> 翻译+校对:[shanks](http://codebuild.me) +> 翻译+校对:[shanks](http://codebuild.me) +> +> 2.2 +> 校对:[SketchK](https://github.com/SketchK) 2016-05-13 本页包含内容: @@ -233,6 +236,6 @@ print("AutomaticCar: \(automatic.description)") 你可以通过把方法,属性或下标标记为*`final`*来防止它们被重写,只需要在声明关键字前加上`final`修饰符即可(例如:`final var`,`final func`,`final class func`,以及`final subscript`)。 -如果你重写了`final`方法,属性或下标,在编译时会报错。在类扩展中的方法,属性或下标也可以在扩展的定义里标记为 final 的。 +如果你重写了带有`final`标记的方法,属性或下标,在编译时会报错。在类扩展中的方法,属性或下标也可以在扩展的定义里标记为 final 的。 你可以通过在关键字`class`前添加`final`修饰符(`final class`)来将整个类标记为 final 的。这样的类是不可被继承的,试图继承这样的类会导致编译报错。