Skip to content

Commit c6e6fb5

Browse files
author
rose.ding
committed
Update Methods.md
Instance method detail
1 parent 8fe2778 commit c6e6fb5

File tree

1 file changed

+35
-69
lines changed

1 file changed

+35
-69
lines changed

swift-6-beta.docc/LanguageGuide/Methods.md

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,15 @@ class Counter {
5757
```
5858
-->
5959

60-
The `Counter` class defines three instance methods:
60+
`Counter` 类定义了三个实例方法:
6161

62-
- `increment()` increments the counter by `1`.
63-
- `increment(by: Int)` increments the counter by a specified integer amount.
64-
- `reset()` resets the counter to zero.
62+
- `increment()` 将计数器增加 `1`
63+
- `increment(by: Int)` 将计数器增加指定的整数值。
64+
- `reset()` 将计数器重置为零。
6565

66-
The `Counter` class also declares a variable property, `count`,
67-
to keep track of the current counter value.
66+
`Counter` 类还声明了一个变量属性 `count`,用于跟踪当前的计数器值。
6867

69-
You call instance methods with the same dot syntax as properties:
68+
你可以使用与属性相同的点语法来调用实例方法:
7069

7170
```swift
7271
let counter = Counter()
@@ -98,20 +97,15 @@ counter.reset()
9897
```
9998
-->
10099

101-
Function parameters can have both a name (for use within the function's body)
102-
and an argument label (for use when calling the function),
103-
as described in <doc:Functions#Function-Argument-Labels-and-Parameter-Names>.
104-
The same is true for method parameters,
105-
because methods are just functions that are associated with a type.
100+
函数参数可以同时拥有一个参数名称(在函数体内使用)和一个参数标签(在调用函数时使用),参见<doc:Functions#Function-Argument-Labels-and-Parameter-Names>
101+
方法参数也是如此,因为方法只是与某种类型关联的函数。
106102

107-
### The self Property
103+
### self 属性
108104

109-
Every instance of a type has an implicit property called `self`,
110-
which is exactly equivalent to the instance itself.
111-
You use the `self` property to refer to the current instance
112-
within its own instance methods.
105+
每个类型的实例都有一个隐式属性,称为 `self`,它与实例本身完全等同。
106+
你可以在实例方法中使用 `self` 属性来引用当前实例。
113107

114-
The `increment()` method in the example above could have been written like this:
108+
上面示例中的 `increment()` 方法可以这样编写:
115109

116110
```swift
117111
func increment() {
@@ -136,22 +130,14 @@ func increment() {
136130
NOTE: I'm slightly cheating with my testing of this excerpt, but it works!
137131
-->
138132

139-
In practice, you don't need to write `self` in your code very often.
140-
If you don't explicitly write `self`,
141-
Swift assumes that you are referring to a property or method of the current instance
142-
whenever you use a known property or method name within a method.
143-
This assumption is demonstrated by the use of `count` (rather than `self.count`)
144-
inside the three instance methods for `Counter`.
133+
实际上,在代码中你不需要经常写 `self`
134+
如果你没有显式地写出 `self`,Swift会假定你在方法内部使用已知的属性或方法名时,指的是当前实例的属性或方法。
135+
这种假设在 `Counter` 类的三个实例方法中通过直接使用 `count`(而非 `self.count`)得到了体现。
145136

146-
The main exception to this rule occurs when a parameter name for an instance method
147-
has the same name as a property of that instance.
148-
In this situation, the parameter name takes precedence,
149-
and it becomes necessary to refer to the property in a more qualified way.
150-
You use the `self` property to
151-
distinguish between the parameter name and the property name.
137+
此规则的主要例外情况发生在实例方法的形参名称与该实例的属性名称相同时。
138+
在这种情况下,形参名称优先,此时就需要以更明确的方式引用属性。你可以使用 `self` 属性来区分形参名称和属性名称。
152139

153-
Here, `self` disambiguates between
154-
a method parameter called `x` and an instance property that's also called `x`:
140+
在如下情况中,`self` 用于区分一个名为 x 的方法形参和一个同样名为 x 的实例属性:
155141

156142
```swift
157143
struct Point {
@@ -185,29 +171,21 @@ if somePoint.isToTheRightOf(x: 1.0) {
185171
```
186172
-->
187173

188-
Without the `self` prefix,
189-
Swift would assume that both uses of `x` referred to the method parameter called `x`.
174+
如果没有 `self` 前缀,Swift 会假定 `x` 的两个使用都指的是名为 `x` 的方法形参。
190175

191-
### Modifying Value Types from Within Instance Methods
176+
### 从实例方法内部修改值类型
192177

193-
Structures and enumerations are *value types*.
194-
By default, the properties of a value type can't be modified from within its instance methods.
178+
结构体和枚举是 *值类型*。默认情况下,值类型的属性不能在其实例方法内部被修改。
195179

196180
<!--
197181
TODO: find out why. once I actually know why, explain it.
198182
-->
199183

200-
However, if you need to modify the properties of your structure or enumeration
201-
within a particular method,
202-
you can opt in to *mutating* behavior for that method.
203-
The method can then mutate (that is, change)
204-
its properties from within the method,
205-
and any changes that it makes are written back to the original structure when the method ends.
206-
The method can also assign a completely new instance to its implicit `self` property,
207-
and this new instance will replace the existing one when the method ends.
184+
然而,如果你需要在特定方法内部修改结构体或枚举的属性,你可以为该方法启用 *mutating* 行为。
185+
这样方法就可以在内部改变其属性,并且所有的更改在方法结束时会写回到原始结构体中。
186+
该方法还可以将一个全新的实例赋值给隐式的 `self` 属性,并且这个新实例将在方法结束时替换现有实例。
208187

209-
You can opt in to this behavior by placing the `mutating` keyword
210-
before the `func` keyword for that method:
188+
你可以通过在该方法的 `func` 关键字前添加 `mutating` 关键字来启用这种行为:
211189

212190
```swift
213191
struct Point {
@@ -241,16 +219,11 @@ print("The point is now at (\(somePoint.x), \(somePoint.y))")
241219
```
242220
-->
243221

244-
The `Point` structure above defines a mutating `moveBy(x:y:)` method,
245-
which moves a `Point` instance by a certain amount.
246-
Instead of returning a new point,
247-
this method actually modifies the point on which it's called.
248-
The `mutating` keyword is added to its definition
249-
to enable it to modify its properties.
222+
上面的 `Point` 结构体定义了一个 mutating 的 `moveBy(x:y:)` 方法,该方法可以将一个 `Point` 实例移动一定的距离。
223+
这个方法直接修改其调用的点,而不是返回一个新的点。为了使该方法能够修改其属性,`mutating` 关键字被添加到它的定义中。
250224

251-
Note that you can't call a mutating method on a constant of structure type,
252-
because its properties can't be changed, even if they're variable properties,
253-
as described in <doc:Properties#Stored-Properties-of-Constant-Structure-Instances>:
225+
请注意,你不能在结构体类型的常量上调用 mutating 方法,因为其属性不能被改变,即使它们是可变属性,
226+
参见:<doc:Properties#Stored-Properties-of-Constant-Structure-Instances>
254227

255228
```swift
256229
let fixedPoint = Point(x: 3.0, y: 3.0)
@@ -289,10 +262,9 @@ fixedPoint.moveBy(x: 2.0, y: 3.0)
289262
if the setter for the computed property is explicitly defined as @!mutating.
290263
-->
291264

292-
### Assigning to self Within a Mutating Method
265+
### 在 mutating 方法中给 self 赋值
293266

294-
Mutating methods can assign an entirely new instance to the implicit `self` property.
295-
The `Point` example shown above could have been written in the following way instead:
267+
mutating 方法可以将一个全新的实例赋值给隐式的 `self` 属性。上面的 `Point` 示例可以改写为以下方式:
296268

297269
```swift
298270
struct Point {
@@ -320,13 +292,10 @@ struct Point {
320292
```
321293
-->
322294

323-
This version of the mutating `moveBy(x:y:)` method creates a new structure
324-
whose `x` and `y` values are set to the target location.
325-
The end result of calling this alternative version of the method
326-
will be exactly the same as for calling the earlier version.
295+
这个版本的 mutating `moveBy(x:y:)` 方法创建了一个新的结构体,其 `x``y` 值被设置为目标位置。
296+
调用这个替代版本方法与调用早期版本方法的最终结果完全相同。
327297

328-
Mutating methods for enumerations can set the implicit `self` parameter to be
329-
a different case from the same enumeration:
298+
枚举的 mutating 方法可以将隐式的 `self` 参数设置为同一枚举的不同case:
330299

331300
```swift
332301
enum TriStateSwitch {
@@ -374,10 +343,7 @@ ovenLight.next()
374343
```
375344
-->
376345

377-
This example defines an enumeration for a three-state switch.
378-
The switch cycles between three different power states
379-
(`off`, `low` and `high`)
380-
every time its `next()` method is called.
346+
这个示例定义了一个三状态开关的枚举。每次调用其 `next()` 方法时,开关将在三种不同的电源状态(`off`, `low``high`)之间循环切换。
381347

382348
## Type Methods
383349

0 commit comments

Comments
 (0)