Skip to content

Commit 8fe2778

Browse files
author
rose.ding
committed
Update Methods.md
Translate Methods and Instance Methods.
1 parent 05d62a3 commit 8fe2778

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

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

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
# Methods
2-
3-
Define and call functions that are part of an instance or type.
4-
5-
*Methods* are functions that are associated with a particular type.
6-
Classes, structures, and enumerations can all define instance methods,
7-
which encapsulate specific tasks and functionality for working with an instance of a given type.
8-
Classes, structures, and enumerations can also define type methods,
9-
which are associated with the type itself.
10-
Type methods are similar to class methods in Objective-C.
11-
12-
The fact that structures and enumerations can define methods in Swift
13-
is a major difference from C and Objective-C.
14-
In Objective-C, classes are the only types that can define methods.
15-
In Swift, you can choose whether to define a class, structure, or enumeration,
16-
and still have the flexibility to define methods on the type you create.
17-
18-
## Instance Methods
19-
20-
*Instance methods* are functions that belong to instances of
21-
a particular class, structure, or enumeration.
22-
They support the functionality of those instances,
23-
either by providing ways to access and modify instance properties,
24-
or by providing functionality related to the instance's purpose.
25-
Instance methods have exactly the same syntax as functions,
26-
as described in <doc:Functions>.
27-
28-
You write an instance method within the opening and closing braces of the type it belongs to.
29-
An instance method has implicit access to all other instance methods and properties of that type.
30-
An instance method can be called only on a specific instance of the type it belongs to.
31-
It can't be called in isolation without an existing instance.
32-
33-
Here's an example that defines a simple `Counter` class,
34-
which can be used to count the number of times an action occurs:
1+
# 方法
2+
3+
定义并调用属于实例或类型的函数。
4+
5+
*方法*是与特定类型关联的函数。
6+
类、结构体和枚举都可以定义实例方法,这些方法封装了处理给定类型实例的特定任务和功能。
7+
类、结构体和枚举还可以定义类型方法,这些方法与类型本身相关联。
8+
类型方法类似于 Objective-C 中的类方法。
9+
10+
结构体和枚举在 Swift 中能够定义方法,这是与 C 和 Objective-C 的一个重大区别。
11+
在 Objective-C 中,只有类可以定义方法。
12+
而在 Swift 中,无论你选择定义类、结构体或枚举,你都可以灵活地为你创建的类型定义方法。
13+
14+
## 实例方法
15+
16+
*实例方法*是属于某个类、结构体或枚举实例的函数。
17+
它们通过提供访问和修改实例属性的方法,或者提供与实例功能相关的操作,来支持实例的整体功能。
18+
实例方法的语法与函数完全相同,具体描述可以参见 <doc:Functions>
19+
20+
你在定义类型时,将实例方法写在类型的大括号内。
21+
实例方法可以隐式访问该类型的所有其他实例方法和属性。
22+
实例方法只能在该类型的特定实例上调用,而不能在没有实例的情况下独立调用。
23+
24+
这是一个定义简单“Counter”类的示例,可用于计算某个动作发生的次数:
3525

3626
```swift
3727
class Counter {

0 commit comments

Comments
 (0)