13
13
> 注意: 类的实例我们通常称为* 对象* 。
14
14
> 但是,与其他语言相比,Swift 中的* 结构体* 和* 类* 在功能上更为接近,
15
15
> 本章大部分介绍的功能都适用于结构体* 或* 类的实例,
16
- > 因此,本章使用了更通用的术语* 实例* 。
16
+ > 因此,本章使用了更通用的术语: * 实例* 。
17
17
18
18
## 比较结构体和类
19
19
@@ -64,7 +64,7 @@ class SomeClass {
64
64
```
65
65
66
66
<!--
67
- - 测试 : `ClassesAndStructures`
67
+ - test : `ClassesAndStructures`
68
68
69
69
```swifttest
70
70
-> struct SomeStructure {
@@ -101,7 +101,7 @@ class VideoMode {
101
101
```
102
102
103
103
<!--
104
- - 测试 : `ClassesAndStructures`
104
+ - test : `ClassesAndStructures`
105
105
106
106
```swifttest
107
107
-> struct Resolution {
@@ -126,29 +126,29 @@ class VideoMode {
126
126
上面的示例还定义了一个名为 ` VideoMode ` 的类,
127
127
用于描述视频显示的特定视频模式。
128
128
该结构体有四个变量存储属性。
129
- 第一个 ` resolution ` 被初始化为一个新的 ` Resolution ` 结构实例 ,
129
+ 第一个 ` resolution ` 被初始化为一个新的 ` Resolution ` 结构体实例 ,
130
130
它的属性类型被推断为 ` Resolution ` 。
131
131
新的 ` VideoMode ` 实例还会初始化其他三个属性。
132
132
分别是初始化为 ` false ` 的 ` interlaced ` (表示 “非隔行扫描视频”)、
133
133
初始化为 ` 0.0 ` 的播放帧率和一个值为可选 ` String ` 的 ` name ` 。
134
134
由于 ` name ` 属性是可选类型,它会自动获得默认值 ` nil ` ,又称 “空 ` name ` 值”。
135
135
136
- ### 结构和类的实例
136
+ ### 结构体和类的实例
137
137
138
- ` Resolution ` 结构定义和 ` VideoMode ` 类定义本身
138
+ ` Resolution ` 结构体定义和 ` VideoMode ` 类定义本身
139
139
只描述了 ` Resolution ` 或 ` VideoMode ` 将是什么样子。
140
140
它们并不描述特定的分辨率或视频模式。
141
- 要做到这一点,你需要创建结构的实例 。
141
+ 要做到这一点,你需要创建结构体的实例 。
142
142
143
- 创建实例的语法对于结构和类都非常相似 :
143
+ 创建实例的语法对于结构体和类都非常相似 :
144
144
145
145
``` swift
146
146
let someResolution = Resolution ()
147
147
let someVideoMode = VideoMode ()
148
148
```
149
149
150
150
<!--
151
- - 测试 : `ClassesAndStructures`
151
+ - test : `ClassesAndStructures`
152
152
153
153
```swifttest
154
154
-> let someResolution = Resolution()
@@ -157,14 +157,14 @@ let someVideoMode = VideoMode()
157
157
-->
158
158
159
159
结构体和类都使用构造器语法来创建新实例。
160
- 最简单的初始化语法形式是使用结构的类型名后跟空括号 ,
160
+ 最简单的初始化语法形式是使用结构体的类型名后跟空括号 ,
161
161
例如 ` Resolution() ` 或 ` VideoMode() ` 。
162
162
这将创建类或结构体的新实例,并将任何属性初始化为其默认值。
163
163
类和结构体的初始化在 < doc:Initialization > 中有更详细的描述。
164
164
165
165
<!--
166
166
TODO: 需要注意的是,
167
- 只有为结构或类的所有属性提供默认值时 ,才能使用默认构造函数。
167
+ 只有为结构体或类的所有属性提供默认值时 ,才能使用默认构造函数。
168
168
-->
169
169
170
170
### 访问属性
@@ -179,7 +179,7 @@ print("The width of someResolution is \(someResolution.width)")
179
179
```
180
180
181
181
<!--
182
- - 测试 : `ClassesAndStructures`
182
+ - test : `ClassesAndStructures`
183
183
184
184
```swifttest
185
185
-> print("The width of someResolution is \(someResolution.width)")
@@ -195,11 +195,12 @@ print("The width of someResolution is \(someResolution.width)")
195
195
例如 ` VideoMode ` 中 ` resolution ` 属性的 ` width ` 属性:
196
196
197
197
``` 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"
199
200
```
200
201
201
202
<!--
202
- - 测试 : `ClassesAndStructures`
203
+ - test : `ClassesAndStructures`
203
204
204
205
```swifttest
205
206
-> print("The width of someVideoMode is \(someVideoMode.resolution.width)")
@@ -216,7 +217,7 @@ print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
216
217
```
217
218
218
219
<!--
219
- - 测试 : `ClassesAndStructures`
220
+ - test : `ClassesAndStructures`
220
221
221
222
```swifttest
222
223
-> someVideoMode.resolution.width = 1280
@@ -228,15 +229,15 @@ print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
228
229
### 结构体逐一成员构造器
229
230
230
231
所有结构体都有一个自动生成的* 逐一成员构造器* ,
231
- 你可以使用它来初始化新结构实例的成员属性 。
232
+ 你可以使用它来初始化新结构体实例的成员属性 。
232
233
可以通过属性成员名称将新实例的属性初始值传递给成员构造器:
233
234
234
235
``` swift
235
236
let vga = Resolution (width : 640 , height : 480 )
236
237
```
237
238
238
239
<!--
239
- - 测试 : `ClassesAndStructures`
240
+ - test : `ClassesAndStructures`
240
241
241
242
```swifttest
242
243
-> let vga = Resolution(width: 640, height: 480)
@@ -247,7 +248,7 @@ let vga = Resolution(width: 640, height: 480)
247
248
构造器在 < doc:Initialization > 中有更详细的描述。
248
249
249
250
<!--
250
- - 测试 : `classesDontHaveADefaultMemberwiseInitializer`
251
+ - test : `classesDontHaveADefaultMemberwiseInitializer`
251
252
252
253
```swifttest
253
254
-> class C { var x = 0, y = 0 }
@@ -259,7 +260,7 @@ let vga = Resolution(width: 640, height: 480)
259
260
```
260
261
-->
261
262
262
- ## 结构和枚举是值类型
263
+ ## 结构体和枚举是值类型
263
264
264
265
* 值类型* 是一种在被赋值给变量或常量时,
265
266
或者在传递给函数时,其值会被* 复制* 的类型。
@@ -277,8 +278,8 @@ let vga = Resolution(width: 640, height: 480)
277
278
在 Swift 中,所有的结构体和枚举都是值类型。
278
279
这意味着它们的实例,以及实例中所包含的任何值类型的属性在代码中传递时都会被复制。
279
280
280
- > 注意: 由 Swift 标准库定义的集合如数组 、字典和字符串
281
- > 优化了实现来减少复制的性能开销 。它们不会立即进行复制,
281
+ > 注意: Swift 标准库定义的集合类型,如数组 、字典和字符串,
282
+ > 使用了一种优化技术来降低复制操作的性能消耗 。它们不会立即进行复制,
282
283
> 而是共享了原始实例和任何副本之间存储元素的内存。
283
284
> 如果集合的某个副本被修改,则在修改之前会复制元素。
284
285
> 您在代码中看到的样子就像立即进行了复制一样。
@@ -291,7 +292,7 @@ var cinema = hd
291
292
```
292
293
293
294
<!--
294
- - 测试 : `ClassesAndStructures`
295
+ - test : `ClassesAndStructures`
295
296
296
297
```swifttest
297
298
-> let hd = Resolution(width: 1920, height: 1080)
@@ -316,7 +317,7 @@ cinema.width = 2048
316
317
```
317
318
318
319
<!--
319
- - 测试 : `ClassesAndStructures`
320
+ - test : `ClassesAndStructures`
320
321
321
322
```swifttest
322
323
-> cinema.width = 2048
@@ -331,7 +332,7 @@ print("cinema is now \(cinema.width) pixels wide")
331
332
```
332
333
333
334
<!--
334
- - 测试 : `ClassesAndStructures`
335
+ - test : `ClassesAndStructures`
335
336
336
337
```swifttest
337
338
-> print("cinema is now \(cinema.width) pixels wide")
@@ -347,7 +348,7 @@ print("hd is still \(hd.width) pixels wide")
347
348
```
348
349
349
350
<!--
350
- - 测试 : `ClassesAndStructures`
351
+ - test : `ClassesAndStructures`
351
352
352
353
```swifttest
353
354
-> print("hd is still \(hd.width) pixels wide")
@@ -383,7 +384,7 @@ print("The remembered direction is \(rememberedDirection)")
383
384
```
384
385
385
386
<!--
386
- - 测试 : `ClassesAndStructures`
387
+ - test : `ClassesAndStructures`
387
388
388
389
```swifttest
389
390
-> enum CompassPoint {
@@ -430,7 +431,7 @@ tenEighty.frameRate = 25.0
430
431
```
431
432
432
433
<!--
433
- - 测试 : `ClassesAndStructures`
434
+ - test : `ClassesAndStructures`
434
435
435
436
```swifttest
436
437
-> let tenEighty = VideoMode()
@@ -455,7 +456,7 @@ alsoTenEighty.frameRate = 30.0
455
456
```
456
457
457
458
<!--
458
- - 测试 : `ClassesAndStructures`
459
+ - test : `ClassesAndStructures`
459
460
460
461
```swifttest
461
462
-> let alsoTenEighty = tenEighty
@@ -478,7 +479,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
478
479
```
479
480
480
481
<!--
481
- - 测试 : `ClassesAndStructures`
482
+ - test : `ClassesAndStructures`
482
483
483
484
```swifttest
484
485
-> print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
@@ -494,9 +495,10 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
494
495
相比之下,值类型更容易理解,因为操作同一个值的代码都集中在一起。
495
496
496
497
需要注意的是,` 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 实例的引用。
500
502
所以,改变的是底层 ` VideoMode ` 实例的 ` frameRate ` 属性,
501
503
而不是指向 ` VideoMode ` 的常量引用的值。
502
504
@@ -520,7 +522,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
520
522
变量或传递给函数时,总是会被复制。
521
523
522
524
<!--
523
- - 测试 : `structuresDontSupportTheIdentityOperators`
525
+ - test : `structuresDontSupportTheIdentityOperators`
524
526
525
527
```swifttest
526
528
-> struct S { var x = 0, y = 0 }
@@ -537,7 +539,7 @@ print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
537
539
-->
538
540
539
541
<!--
540
- - 测试 : `enumerationsDontSupportTheIdentityOperators`
542
+ - test : `enumerationsDontSupportTheIdentityOperators`
541
543
542
544
```swifttest
543
545
-> enum E { case a, b }
@@ -569,7 +571,7 @@ if tenEighty === alsoTenEighty {
569
571
```
570
572
571
573
<!--
572
- - 测试 : `ClassesAndStructures`
574
+ - test : `ClassesAndStructures`
573
575
574
576
```swifttest
575
577
-> if tenEighty === alsoTenEighty {
@@ -589,7 +591,7 @@ if tenEighty === alsoTenEighty {
589
591
定义自己的 ` == ` 和 ` != ` 运算符实现的过程在 < doc:AdvancedOperators#Equivalence-Operators > 中有描述。
590
592
591
593
<!--
592
- - 测试 : `classesDontGetEqualityByDefault`
594
+ - test : `classesDontGetEqualityByDefault`
593
595
594
596
```swifttest
595
597
-> class C { var x = 0, y = 0 }
@@ -603,7 +605,7 @@ if tenEighty === alsoTenEighty {
603
605
-->
604
606
605
607
<!--
606
- - 测试 : `structuresDontGetEqualityByDefault`
608
+ - test : `structuresDontGetEqualityByDefault`
607
609
608
610
```swifttest
609
611
-> struct S { var x = 0, y = 0 }
0 commit comments