Skip to content

Commit 2c7348e

Browse files
committed
fix for PR review by @TsnumiDC
1 parent 7e618e7 commit 2c7348e

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
> 注意: 类的实例我们通常称为*对象*
1414
> 但是,与其他语言相比,Swift 中的*结构体***在功能上更为接近,
1515
> 本章大部分介绍的功能都适用于结构体**类的实例,
16-
> 因此,本章使用了更通用的术语*实例*
16+
> 因此,本章使用了更通用的术语*实例*
1717
1818
## 比较结构体和类
1919

@@ -64,7 +64,7 @@ class SomeClass {
6464
```
6565

6666
<!--
67-
- 测试: `ClassesAndStructures`
67+
- test: `ClassesAndStructures`
6868
6969
```swifttest
7070
-> struct SomeStructure {
@@ -101,7 +101,7 @@ class VideoMode {
101101
```
102102

103103
<!--
104-
- 测试: `ClassesAndStructures`
104+
- test: `ClassesAndStructures`
105105
106106
```swifttest
107107
-> struct Resolution {
@@ -126,29 +126,29 @@ class VideoMode {
126126
上面的示例还定义了一个名为 `VideoMode` 的类,
127127
用于描述视频显示的特定视频模式。
128128
该结构体有四个变量存储属性。
129-
第一个 `resolution` 被初始化为一个新的 `Resolution` 结构实例
129+
第一个 `resolution` 被初始化为一个新的 `Resolution` 结构体实例
130130
它的属性类型被推断为 `Resolution`
131131
新的 `VideoMode` 实例还会初始化其他三个属性。
132132
分别是初始化为 `false``interlaced`(表示 “非隔行扫描视频”)、
133133
初始化为 `0.0` 的播放帧率和一个值为可选 `String``name`
134134
由于 `name` 属性是可选类型,它会自动获得默认值 `nil`,又称 “空 `name` 值”。
135135

136-
### 结构和类的实例
136+
### 结构体和类的实例
137137

138-
`Resolution` 结构定义和 `VideoMode` 类定义本身
138+
`Resolution` 结构体定义和 `VideoMode` 类定义本身
139139
只描述了 `Resolution``VideoMode` 将是什么样子。
140140
它们并不描述特定的分辨率或视频模式。
141-
要做到这一点,你需要创建结构的实例
141+
要做到这一点,你需要创建结构体的实例
142142

143-
创建实例的语法对于结构和类都非常相似
143+
创建实例的语法对于结构体和类都非常相似
144144

145145
```swift
146146
let someResolution = Resolution()
147147
let someVideoMode = VideoMode()
148148
```
149149

150150
<!--
151-
- 测试: `ClassesAndStructures`
151+
- test: `ClassesAndStructures`
152152
153153
```swifttest
154154
-> let someResolution = Resolution()
@@ -157,14 +157,14 @@ let someVideoMode = VideoMode()
157157
-->
158158

159159
结构体和类都使用构造器语法来创建新实例。
160-
最简单的初始化语法形式是使用结构的类型名后跟空括号
160+
最简单的初始化语法形式是使用结构体的类型名后跟空括号
161161
例如 `Resolution()``VideoMode()`
162162
这将创建类或结构体的新实例,并将任何属性初始化为其默认值。
163163
类和结构体的初始化在 <doc:Initialization> 中有更详细的描述。
164164

165165
<!--
166166
TODO: 需要注意的是,
167-
只有为结构或类的所有属性提供默认值时,才能使用默认构造函数。
167+
只有为结构体或类的所有属性提供默认值时,才能使用默认构造函数。
168168
-->
169169

170170
### 访问属性
@@ -179,7 +179,7 @@ print("The width of someResolution is \(someResolution.width)")
179179
```
180180

181181
<!--
182-
- 测试: `ClassesAndStructures`
182+
- test: `ClassesAndStructures`
183183
184184
```swifttest
185185
-> print("The width of someResolution is \(someResolution.width)")
@@ -195,11 +195,12 @@ print("The width of someResolution is \(someResolution.width)")
195195
例如 `VideoMode``resolution` 属性的 `width` 属性:
196196

197197
```swift
198-
print("The width of someVideoMode is \(someVideoMode.resolution.width)") // 打印 "The width of someVideoMode is 0"
198+
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
199+
// 打印 "The width of someVideoMode is 0"
199200
```
200201

201202
<!--
202-
- 测试: `ClassesAndStructures`
203+
- test: `ClassesAndStructures`
203204
204205
```swifttest
205206
-> print("The width of someVideoMode is \(someVideoMode.resolution.width)")
@@ -216,7 +217,7 @@ print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
216217
```
217218

218219
<!--
219-
- 测试: `ClassesAndStructures`
220+
- test: `ClassesAndStructures`
220221
221222
```swifttest
222223
-> someVideoMode.resolution.width = 1280
@@ -228,15 +229,15 @@ print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
228229
### 结构体逐一成员构造器
229230

230231
所有结构体都有一个自动生成的*逐一成员构造器*
231-
你可以使用它来初始化新结构实例的成员属性
232+
你可以使用它来初始化新结构体实例的成员属性
232233
可以通过属性成员名称将新实例的属性初始值传递给成员构造器:
233234

234235
```swift
235236
let vga = Resolution(width: 640, height: 480)
236237
```
237238

238239
<!--
239-
- 测试: `ClassesAndStructures`
240+
- test: `ClassesAndStructures`
240241
241242
```swifttest
242243
-> let vga = Resolution(width: 640, height: 480)
@@ -247,7 +248,7 @@ let vga = Resolution(width: 640, height: 480)
247248
构造器在 <doc:Initialization> 中有更详细的描述。
248249

249250
<!--
250-
- 测试: `classesDontHaveADefaultMemberwiseInitializer`
251+
- test: `classesDontHaveADefaultMemberwiseInitializer`
251252
252253
```swifttest
253254
-> class C { var x = 0, y = 0 }
@@ -259,7 +260,7 @@ let vga = Resolution(width: 640, height: 480)
259260
```
260261
-->
261262

262-
## 结构和枚举是值类型
263+
## 结构体和枚举是值类型
263264

264265
*值类型*是一种在被赋值给变量或常量时,
265266
或者在传递给函数时,其值会被*复制*的类型。
@@ -277,8 +278,8 @@ let vga = Resolution(width: 640, height: 480)
277278
在 Swift 中,所有的结构体和枚举都是值类型。
278279
这意味着它们的实例,以及实例中所包含的任何值类型的属性在代码中传递时都会被复制。
279280

280-
> 注意: Swift 标准库定义的集合如数组、字典和字符串
281-
> 优化了实现来减少复制的性能开销。它们不会立即进行复制,
281+
> 注意: Swift 标准库定义的集合类型,如数组、字典和字符串
282+
> 使用了一种优化技术来降低复制操作的性能消耗。它们不会立即进行复制,
282283
> 而是共享了原始实例和任何副本之间存储元素的内存。
283284
> 如果集合的某个副本被修改,则在修改之前会复制元素。
284285
> 您在代码中看到的样子就像立即进行了复制一样。
@@ -291,7 +292,7 @@ var cinema = hd
291292
```
292293

293294
<!--
294-
- 测试: `ClassesAndStructures`
295+
- test: `ClassesAndStructures`
295296
296297
```swifttest
297298
-> let hd = Resolution(width: 1920, height: 1080)
@@ -316,7 +317,7 @@ cinema.width = 2048
316317
```
317318

318319
<!--
319-
- 测试: `ClassesAndStructures`
320+
- test: `ClassesAndStructures`
320321
321322
```swifttest
322323
-> cinema.width = 2048
@@ -331,7 +332,7 @@ print("cinema is now \(cinema.width) pixels wide")
331332
```
332333

333334
<!--
334-
- 测试: `ClassesAndStructures`
335+
- test: `ClassesAndStructures`
335336
336337
```swifttest
337338
-> print("cinema is now \(cinema.width) pixels wide")
@@ -347,7 +348,7 @@ print("hd is still \(hd.width) pixels wide")
347348
```
348349

349350
<!--
350-
- 测试: `ClassesAndStructures`
351+
- test: `ClassesAndStructures`
351352
352353
```swifttest
353354
-> print("hd is still \(hd.width) pixels wide")
@@ -383,7 +384,7 @@ print("The remembered direction is \(rememberedDirection)")
383384
```
384385

385386
<!--
386-
- 测试: `ClassesAndStructures`
387+
- test: `ClassesAndStructures`
387388
388389
```swifttest
389390
-> enum CompassPoint {
@@ -430,7 +431,7 @@ tenEighty.frameRate = 25.0
430431
```
431432

432433
<!--
433-
- 测试: `ClassesAndStructures`
434+
- test: `ClassesAndStructures`
434435
435436
```swifttest
436437
-> let tenEighty = VideoMode()
@@ -455,7 +456,7 @@ alsoTenEighty.frameRate = 30.0
455456
```
456457

457458
<!--
458-
- 测试: `ClassesAndStructures`
459+
- test: `ClassesAndStructures`
459460
460461
```swifttest
461462
-> let alsoTenEighty = tenEighty
@@ -478,7 +479,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
478479
```
479480

480481
<!--
481-
- 测试: `ClassesAndStructures`
482+
- test: `ClassesAndStructures`
482483
483484
```swifttest
484485
-> print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
@@ -494,9 +495,10 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
494495
相比之下,值类型更容易理解,因为操作同一个值的代码都集中在一起。
495496

496497
需要注意的是,`tenEighty``alsoTenEighty` 虽然被声明为常量而不是变量,
497-
但你任然可以修改 `tenEighty.frameRate``alsoTenEighty.frameRate`
498-
因为 `tenEighty``alsoTenEighty` 本身常量的值并没有变化。`VideoMode` 实例,
499-
而是对 `VideoMode` 的引用。
498+
但你仍然可以修改 `tenEighty.frameRate``alsoTenEighty.frameRate`
499+
tenEighty 和 alsoTenEighty 常量的值本身并没有变化。
500+
它们并不“存储”这个 VideoMode 实例,
501+
而仅仅是对 VideoMode 实例的引用。
500502
所以,改变的是底层 `VideoMode` 实例的 `frameRate` 属性,
501503
而不是指向 `VideoMode` 的常量引用的值。
502504

@@ -520,7 +522,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
520522
变量或传递给函数时,总是会被复制。
521523

522524
<!--
523-
- 测试: `structuresDontSupportTheIdentityOperators`
525+
- test: `structuresDontSupportTheIdentityOperators`
524526
525527
```swifttest
526528
-> struct S { var x = 0, y = 0 }
@@ -537,7 +539,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
537539
-->
538540

539541
<!--
540-
- 测试: `enumerationsDontSupportTheIdentityOperators`
542+
- test: `enumerationsDontSupportTheIdentityOperators`
541543
542544
```swifttest
543545
-> enum E { case a, b }
@@ -569,7 +571,7 @@ if tenEighty === alsoTenEighty {
569571
```
570572

571573
<!--
572-
- 测试: `ClassesAndStructures`
574+
- test: `ClassesAndStructures`
573575
574576
```swifttest
575577
-> if tenEighty === alsoTenEighty {
@@ -589,7 +591,7 @@ if tenEighty === alsoTenEighty {
589591
定义自己的 `==``!=` 运算符实现的过程在 <doc:AdvancedOperators#Equivalence-Operators> 中有描述。
590592

591593
<!--
592-
- 测试: `classesDontGetEqualityByDefault`
594+
- test: `classesDontGetEqualityByDefault`
593595
594596
```swifttest
595597
-> class C { var x = 0, y = 0 }
@@ -603,7 +605,7 @@ if tenEighty === alsoTenEighty {
603605
-->
604606

605607
<!--
606-
- 测试: `structuresDontGetEqualityByDefault`
608+
- test: `structuresDontGetEqualityByDefault`
607609
608610
```swifttest
609611
-> struct S { var x = 0, y = 0 }

0 commit comments

Comments
 (0)