static vs class as class variable / method(Swift) [英] static vs class as class variable/method (Swift)
问题描述
我知道 static
关键字用于在 struct
中声明类型变量/方法, enum
等。
I know that static
keyword is used to declare type variable/method in struct
, enum
etc.
但今天我发现它也可用于 class
实体。
But today I found it can also be used in class
entity.
class foo {
static func hi() {
println("hi")
}
class func hello() {
println("hello")
}
}
什么是静态
关键字在类
实体中的使用?
What's static
keyword's use in class
entity?
谢谢!
编辑:我指的是Swift 1.2,如果这有任何区别
edit: I'm referring to Swift 1.2 if that makes any difference
推荐答案
从Xcode 3 beta 3发行说明:
From the Xcode 3 beta 3 release notes:
静态方法和属性现在允许在类中(作为类最终的
别名)。
"static" methods and properties are now allowed in classes (as an alias for "class final").
所以在Swift 1.2, hi()
定义为
So in Swift 1.2, hi()
defined as
class foo {
static func hi() {
println("hi")
}
}
是类型方法(即在类型本身上调用的方法)
也是 final (即不能在子类中重写)。
is a type method (i.e. a method that is called on the type itself) which also is final (i.e. cannot be overridden in a subclass).
这篇关于static vs class as class variable / method(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!