swift中的类型方法

来源:互联网 发布:js json对象 动态key 编辑:程序博客网 时间:2024/06/09 18:21

定义
Instance methods, as described above, are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.
实例方法,正如上边提到的,是那些被实例调用的方法。你也可以定义一些类型自己调用的方法。这种方法被称作类方法。你可以表明是类方法,在方法前加上static关键字。类也可以应用class关键字允许子类重写父类的实现。
这里写图片描述
注意
在oc中,你仅仅可以为类定义类型方法。在swift中,你可以类型方法为所有的类,结构体和枚举。每种类型方法都显示的由类型来限定应用范围。
Within the body of a type method, the implicit self property refers to the type itself, rather than an instance of that type. This means that you can use self to disambiguate between type properties and type method parameters, just as you do for instance properties and instance method parameters.
在类型方法内部,隐式的self指的是类型自己而不是实例。这意味着你可以应用self来区分参数还是属性。
More generally, any unqualified method and property names that you use within the body of a type method will refer to other type-level methods and properties. A type method can call another type method with the other method’s name, without needing to prefix it with the type name. Similarly, type methods on structures and enumerations can access type properties by using the type property’s name without a type name prefix.
大多数情况下,在类方法内部你应用的方法和属性可以引用其他的方法和属性。结构体和枚举也具有同样的功能。

0 0
原创粉丝点击