Swift类中的静态vs类函数/变量?

本文翻译自:Static vs class functions/variables in Swift classes?

The following code compiles in Swift 1.2: 以下代码在Swift 1.2中进行编译:

class myClass {
    static func myMethod1() {
    }
    class func myMethod2() {
    }
    static var myVar1 = ""
}

func doSomething() {
    myClass.myMethod1()
    myClass.myMethod2()
    myClass.myVar1 = "abc"
}

What is the difference between a static function and a class function? 静态函数和函数有什么区别? Which one should I use, and when? 我应该使用哪一个?何时使用?

If I try to define another variable class var myVar2 = "" , it says: 如果我尝试定义另一个变量class var myVar2 = "" ,它说:

Class stored properties not yet supported in classes; 类中尚不支持的类存储属性; did you mean 'static'? 您是说“静态”吗?

When this feature is supported, what will the difference be between a static variable and a class variable (ie when both are defined in a class)? 如果支持此功能,则静态变量和变量之间有什么区别(即,当两者都在类中定义时)? Which one should I use, and when? 我应该使用哪一个?何时使用?

(Xcode 6.3) (Xcode 6.3)


#1楼

参考:https://stackoom.com/question/20LqD/Swift类中的静态vs类函数-变量


#2楼

static and class both associate a method with a class, rather than an instance of a class. staticclass都将方法与类而不是类的实例相关联。 The difference is that subclasses can override class methods; 不同之处在于子类可以覆盖class方法。 they cannot override static methods. 他们不能覆盖static方法。

class properties will theoretically function in the same way (subclasses can override them), but they're not possible in Swift yet. class理论上讲, class属性将以相同的方式起作用(子类可以覆盖它们),但是在Swift中尚无法实现。


#3楼

Regarding to OOP , the answer is too simple: 关于OOP ,答案太简单了:

The subclasses can override class methods, but cannot override static methods. 子类可以覆盖方法,但不能覆盖静态方法。

In addition to your post, if you want to declare a class variable (like you did class var myVar2 = "" ), you should do it as follow: 除了您的文章之外,如果您想声明一个变量(就像您对class var myVar2 = ""所做的一样),还应按以下步骤进行操作:

class var myVar2: String {
    return "whatever you want"
}

#4楼

I tried mipadi's answer and comments on playground. 我在操场上尝试了mipadi的回答和评论。 And thought of sharing it. 并考虑共享它。 Here you go. 干得好。 I think mipadi's answer should be mark as accepted. 我认为mipadi的答案应标记为已接受。

class A{
    class func classFunction(){
    }
    static func staticFunction(){
    }
    class func classFunctionToBeMakeFinalInImmediateSubclass(){
    }
}

class B: A {
    override class func classFunction(){

    }

    //Compile Error. Class method overrides a 'final' class method
    override static func staticFunction(){

    }

    //Lets avoid the function called 'classFunctionToBeMakeFinalInImmediateSubclass' being overriden by subclasses

    /* First way of doing it
    override static func classFunctionToBeMakeFinalInImmediateSubclass(){
    }
    */

    // Second way of doing the same
    override final class func classFunctionToBeMakeFinalInImmediateSubclass(){
    }

    //To use static or final class is choice of style.
    //As mipadi suggests I would use. static at super class. and final class to cut off further overrides by a subclass
}

class C: B{
    //Compile Error. Class method overrides a 'final' class method
    override static func classFunctionToBeMakeFinalInImmediateSubclass(){

    }
}

#5楼

I got this confusion in one of my project as well and found this post, very helpful. 在我的一个项目中,我也感到困惑,发现这篇文章非常有帮助。 Tried the same in my playground and here is the summary. 在我的操场上也尝试过,这里是摘要。 Hope this helps someone with stored properties and functions of type static , final , class , overriding class vars etc. 希望这对具有存储的属性和类型为staticfinalclass ,重写class vars等功能的人有所帮助。

class Simple {

    init() {print("init method called in base")}

    class func one() {print("class - one()")}

    class func two() {print("class - two()")}

    static func staticOne() {print("staticOne()")}

    static func staticTwo() {print("staticTwo()")}

    final func yesFinal() {print("yesFinal()")}

    static var myStaticVar = "static var in base"

    //Class stored properties not yet supported in classes; did you mean 'static'?
    class var myClassVar1 = "class var1"

    //This works fine
    class var myClassVar: String {
       return "class var in base"
    }
}

class SubSimple: Simple {
    //Successful override
    override class func one() {
        print("subClass - one()")
    }
    //Successful override
    override class func two () {
        print("subClass - two()")
    }

    //Error: Class method overrides a 'final' class method
    override static func staticOne() {

    }

    //error: Instance method overrides a 'final' instance method
    override final func yesFinal() {

    }

    //Works fine
    override class var myClassVar: String {
        return "class var in subclass"
    }
}

And here is the testing samples: 这是测试样本:

print(Simple.one())
print(Simple.two())
print(Simple.staticOne())
print(Simple.staticTwo())
print(Simple.yesFinal(Simple()))
print(SubSimple.one())
print(Simple.myStaticVar)
print(Simple.myClassVar)
print(SubSimple.myClassVar)

//Output
class - one()
class - two()
staticOne()
staticTwo()
init method called in base
(Function)
subClass - one()
static var in base
class var in base
class var in subclass

#6楼

There's one more difference. 还有另外一个区别。 class can be used to define type properties of computed type only . class只能用于定义计算类型的类型属性。 If you need a stored type property use static instead. 如果需要存储的类型属性,请使用static

"You define type properties with the static keyword. For computed type properties for class types, you can use the class keyword instead to allow subclasses to override the superclass's implementation." “您可以使用static关键字定义类型属性。对于类类型的计算类型属性,可以使用class关键字来允许子类重写超类的实现。”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值