tiny changes (#859)

This commit is contained in:
Chi Zhang
2019-01-21 10:54:45 +08:00
committed by Jie Liang
parent 1343749921
commit 4c3ad3d2f8
2 changed files with 27 additions and 27 deletions

View File

@ -72,18 +72,18 @@ func increment() {
```swift ```swift
struct Point { struct Point {
var x = 0.0, y = 0.0 var x = 0.0, y = 0.0
func isToTheRightOfX(_ x: Double) -> Bool { func isToTheRightOf(x: Double) -> Bool {
return self.x > x return self.x > x
} }
} }
let somePoint = Point(x: 4.0, y: 5.0) let somePoint = Point(x: 4.0, y: 5.0)
if somePoint.isToTheRightOfX(1.0) { if somePoint.isToTheRightOf(x: 1.0) {
print("This point is to the right of the line where x == 1.0") print("This point is to the right of the line where x == 1.0")
} }
// 打印“This point is to the right of the line where x == 1.0” // 打印“This point is to the right of the line where x == 1.0”
``` ```
如果不使用 `self` 前缀Swift 就认为两次使用的 `x` 都指的是名称`x`函数参数。 如果不使用 `self` 前缀Swift会认为 `x` 的两个用法都引用了名`x`方法参数。
<a name="modifying_value_types_from_within_instance_methods"></a> <a name="modifying_value_types_from_within_instance_methods"></a>
### 在实例方法中修改值类型 ### 在实例方法中修改值类型
@ -97,24 +97,24 @@ if somePoint.isToTheRightOfX(1.0) {
```swift ```swift
struct Point { struct Point {
var x = 0.0, y = 0.0 var x = 0.0, y = 0.0
mutating func moveByX(_ deltaX: Double, y deltaY: Double) { mutating func moveBy(x deltaX: Double, y deltaY: Double) {
x += deltaX x += deltaX
y += deltaY y += deltaY
} }
} }
var somePoint = Point(x: 1.0, y: 1.0) var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveByX(2.0, y: 3.0) somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at (\(somePoint.x), \(somePoint.y))") print("The point is now at (\(somePoint.x), \(somePoint.y))")
// 打印“The point is now at (3.0, 4.0)” // 打印“The point is now at (3.0, 4.0)”
``` ```
上面的 `Point` 结构体定义了一个可变方法 `moveByX(_:y:)` 来移动 `Point` 实例到给定的位置。该方法被调用时修改了这个点,而不是返回一个新的点。方法定义时加上了 `mutating` 关键字,从而允许修改属性。 上面的 `Point` 结构体定义了一个可变方法 `moveByxy :)` 来移动 `Point` 实例到给定的位置。该方法被调用时修改了这个点,而不是返回一个新的点。方法定义时加上了 `mutating` 关键字,从而允许修改属性。
注意不能在结构体类型的常量a constant of structure type上调用可变方法因为其属性不能被改变即使属性是变量属性详情参见[常量结构体的存储属性](./10_Properties.html#stored_properties_of_constant_structure_instances) 注意不能在结构体类型的常量a constant of structure type上调用可变方法因为其属性不能被改变即使属性是变量属性详情参见[常量结构体的存储属性](./10_Properties.html#stored_properties_of_constant_structure_instances)
```swift ```swift
let fixedPoint = Point(x: 3.0, y: 3.0) let fixedPoint = Point(x: 3.0, y: 3.0)
fixedPoint.moveByX(2.0, y: 3.0) fixedPoint.moveBy(x: 2.0, y: 3.0)
// 这里将会报告一个错误 // 这里将会报告一个错误
``` ```
@ -138,26 +138,26 @@ struct Point {
```swift ```swift
enum TriStateSwitch { enum TriStateSwitch {
case Off, Low, High case off, low, high
mutating func next() { mutating func next() {
switch self { switch self {
case .Off: case .off:
self = .Low self = .low
case .Low: case .low:
self = .High self = .high
case .High: case .high:
self = .Off self = .off
} }
} }
} }
var ovenLight = TriStateSwitch.Low var ovenLight = TriStateSwitch.low
ovenLight.next() ovenLight.next()
// ovenLight 现在等于 .High // ovenLight 现在等于 .high
ovenLight.next() ovenLight.next()
// ovenLight 现在等于 .Off // ovenLight 现在等于 .off
``` ```
上面的例子中定义了一个三态开关的枚举。每次调用 `next()` 方法时,开关在不同的电源状态(`Off``Low``High`)之间循环切换。 上面的例子中定义了一个三态切换的枚举。每次调用 `next()` 方法时,开关在不同的电源状态(`off`, `low`, `high`)之间循环切换。
<a name="type_methods"></a> <a name="type_methods"></a>
## 类型方法 ## 类型方法
@ -179,7 +179,7 @@ class SomeClass {
SomeClass.someTypeMethod() SomeClass.someTypeMethod()
``` ```
在类型方法的方法体body`self` 指向这个类型本身,而不是类型的某个实例。这意味着你可以用 `self` 来消除类型属性和类型方法参数之间的歧义(类似于我们在前面处理实例属性和实例方法参数时做的那样)。 在类型方法的方法体body`self` 属性指向这个类型本身,而不是类型的某个实例。这意味着你可以用 `self` 来消除类型属性和类型方法参数之间的歧义(类似于我们在前面处理实例属性和实例方法参数时做的那样)。
一般来说,在类型方法的方法体中,任何未限定的方法和属性名称,可以被本类中其他的类型方法和类型属性引用。一个类型方法可以直接通过类型方法的名称调用本类中的其它类型方法,而无需在方法名称前面加上类型名称。类似地,在结构体和枚举中,也能够直接通过类型属性的名称访问本类中的类型属性,而不需要前面加上类型名称。 一般来说,在类型方法的方法体中,任何未限定的方法和属性名称,可以被本类中其他的类型方法和类型属性引用。一个类型方法可以直接通过类型方法的名称调用本类中的其它类型方法,而无需在方法名称前面加上类型名称。类似地,在结构体和枚举中,也能够直接通过类型属性的名称访问本类中的类型属性,而不需要前面加上类型名称。

View File

@ -155,7 +155,7 @@ private class SomePrivateClass { // 显式 private 类
```swift ```swift
func someFunction() -> (SomeInternalClass, SomePrivateClass) { func someFunction() -> (SomeInternalClass, SomePrivateClass) {
// 此处是函数实现部分 // 此处是函数实现部分
} }
``` ```
@ -165,7 +165,7 @@ func someFunction() -> (SomeInternalClass, SomePrivateClass) {
```swift ```swift
private func someFunction() -> (SomeInternalClass, SomePrivateClass) { private func someFunction() -> (SomeInternalClass, SomePrivateClass) {
// 此处是函数实现部分 // 此处是函数实现部分
} }
``` ```
@ -180,10 +180,10 @@ private func someFunction() -> (SomeInternalClass, SomePrivateClass) {
```swift ```swift
public enum CompassPoint { public enum CompassPoint {
case North case north
case South case south
case East case east
case West case west
} }
``` ```
@ -208,11 +208,11 @@ public enum CompassPoint {
```swift ```swift
public class A { public class A {
private func someMethod() {} private func someMethod() {}
} }
internal class B: A { internal class B: A {
override internal func someMethod() {} override internal func someMethod() {}
} }
``` ```