swift方法

来源:互联网 发布:傲剑八卦升级数据大全 编辑:程序博客网 时间:2024/05/29 16:41

方法定义
Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C.
方法是结合了特定类型的函数。类,结构体,枚举都可以定义实例方法。它们为给定的实例,封装了特定类型的任务和功能。类,枚举,结构体也可以定义定义类型方法。它们和类型绑定在一起。类型方法类似于oc中的类方法。
The fact that structures and enumerations can define methods in Swift is a major difference from C and Objective-C. In Objective-C, classes are the only types that can define methods. In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.
事实上swift中可以在枚举和结构体中可以定义方法是与oc的主要区别。在oc中,类是仅有可以定义方法的类型。在swift中,你可以选择是否定义结构体,类,枚举,他是比较有弹性的为你创建的类型定义结构体。
实例方法定义
Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They support the functionality of those instances, either by providing ways to access and modify instance properties, or by providing functionality related to the instance’s purpose. Instance methods have exactly the same syntax as functions, as described in Functions.
实例方法是属于类,结构体,枚举的实例的函数。它支持市里的特定功能,或者访问和修改实例,或者实例的特定功能。实例方法有类似类似函数的点语法。
You write an instance method within the opening and closing braces of the type it belongs to. An instance method has implicit access to all other instance methods and properties of that type. An instance method can be called only on a specific instance of the type it belongs to. It cannot be called in isolation without an existing instance.
你只需要在实例所属于的类型的一对大括号内部书写实例方法。所有的实例方法都可以隐式地访问其他的实例方法和属性。实例方法仅仅可以被属于的那个实例来进行调用。不能在没有实例的情况下,独自调用。
Function parameters can have both a name (for use within the function’s body) and an argument label (for use when calling the function), as described in Function Argument Labels and Parameter Names. The same is true for method parameters, because methods are just functions that are associated with a type.
方法和函数一样,都可以定义标签和参数名。因为方法就是一种街尅了类型的函数。

0 0
原创粉丝点击