Swift中的静态函数和类函数有什么区别?

本文探讨了Swift中static func和class func的区别。static主要适用于结构体和枚举的静态函数,而class则用于类和协议。类函数是动态调度的,可以被子类覆盖。尽管协议使用class关键字,但结构体仍可以使用static实现协议。Swift选择不同的关键字是为了未来的语言扩展,如可能添加到类中的真正静态方法。此外,还提到了类型变量属性和类的声明修饰符的影响。
摘要由CSDN通过智能技术生成

本文翻译自:What is the difference between static func and class func in Swift?

I can see these definitions in the Swift library: 我可以在Swift库中看到这些定义:

extension Bool : BooleanLiteralConvertible {
    static func convertFromBooleanLiteral(value: Bool) -> Bool
}

protocol BooleanLiteralConvertible {
    typealias BooleanLiteralType
    class func convertFromBooleanLiteral(value: BooleanLiteralType) -> Self
}

What's the difference between a member function defined as static func and another one defined as class func ? 一个成员函数定义为static func和另一个成员函数定义为class func什么区别? Is it simply that static is for static functions of structs and enums, and class for classes and protocols? 是否仅将static用于结构和枚举的静态函数,将class用于类和协议? Are there any other differences that one should know about? 还有其他应该知道的区别吗? What is the rationale for having this distinction in the syntax itself? 在语法本身中具有这种区别的原理是什么?


#1楼

参考:https://stackoom.com/question/1hYK1/Swift中的静态函数和类函数有什么区别


#2楼

Is it simply that static is for static functions of structs and enums, and class for classes and protocols? 是否仅将static用于结构和枚举的静态函数,将class用于类和协议?

That's the main difference. 那是主要的区别。 Some other differences are that class functions are dynamically dispatched and can be overridden by subclasses. 其他一些区别是类函数是动态调度的,并且可以被子类覆盖。

Protocols use the class keyword, but it doesn't exclude structs from implementing the protocol, they just use static instead. 协议使用class关键字,但是它并未从实现协议中排除结构,而是仅使用static。 Class was chosen for protocols so there wouldn't have to be a third keyword to represent static or class. 为协议选择了类,因此不必使用第三个关键字来表示静态或类。

From Chris Lattner on this topic: 来自Chris Lattner的以下主题:

We considered unifying the syntax (eg using "type" as the keyword), but that doesn't actually simply things. 我们考虑统一语法(例如,使用“ type”作为关键字),但这实际上并不是简单的事情。 The keywords "class" and "static" are good for familiarity and are quite descriptive (once you understand how + methods work), and open the door for potentially adding truly static methods to classes. 关键字“类”和“静态”有助于提高熟悉度,并且具有很好的描述性(一旦您了解+方法的工作原理),并为潜在地向类中添加真正的静态方法打开了大门。 The primary weirdness of this model is that protocols have to pick a keyword (and we chose "class"), but on balance it is the right tradeoff. 该模型的主要怪异之处在于协议必须选择一个关键字(而我们选择了“类”),但总的来说,这是正确的权衡。

And here's a snippet that shows some of the override behavior of class functions: 这是显示一些类函数的替代行为的代码段:

class MyClass {
    class func myFunc() {
        println("myClass")
    }
}

class MyOtherClass: MyClass {
    override class func myFunc() {
        println("myOtherClass")
    }
}

var x: MyClass = MyOtherClass()
x.dynamicType.myFunc() //myOtherClass
x = MyClass()
x.dynamicType.myFunc() //myClass

#3楼

To declare a type variable property, mark the declaration with the static declaration modifier. 要声明类型变量属性,请使用static声明修饰符标记声明。 Classes may mark type computed properties with the class declaration modifier instead to allow subclasses to override the superclass's implementation. 类可以使用class声明修饰符标记类型计算的属性,以允许子类覆盖超类的实现。 Type properties are discussed in Type Properties. 类型属性在类型属性中讨论。

NOTE 注意
In a class declaration, the keyword static has the same effect as marking the declaration with both the class and final declaration modifiers. 在类声明中,关键字static与使用classfinal声明修饰符标记该声明具有相同的效果。

Source: The Swift Programming Language - Type Variable Properties 来源: Swift编程语言-类型变量属性


#4楼

From Swift2.0, Apple says: Apple从Swift2.0中说:

"Always prefix type property requirements with the static keyword when you define them in a protocol. This rule pertains even though type property requirements can be prefixed with the class or static keyword when implemented by a class:" “在协议中定义类型属性要求时,始终用static关键字作为前缀。即使由类实现时,类型属性要求可以以class或static关键字作为前缀,该规则也适用:”


#5楼

To be clearer, I make an example here, 更清楚地说,我在这里举例说明

class ClassA {
  class func func1() -> String {
    return "func1"
  }

  static func func2() -> String {
    return "func2"
  }

  /* same as above
  final class func func2() -> String {
    return "func2"
  }
  */
}

static func is same as final class func static funcfinal class func相同

Because it is final , we can not override it in subclass as below: 因为它是final ,所以不能在子类中重写它,如下所示:

class ClassB : ClassA {
  override class func func1() -> String {
    return "func1 in ClassB"
  }

  // ERROR: Class method overrides a 'final` class method
  override static func func2() -> String {
    return "func2 in ClassB"
  }
}

#6楼

the main difference is that structs are value types and classes are reference types. 主要区别在于结构是值类型,类是引用类型。

When you make a copy of a value type, it copies all the data from the thing you are copying into the new variable. 复制值类型时,它会将您要复制的事物中的所有数据复制到新变量中。 They are 2 seperate things and changing one does not affect the other 它们是2个单独的东西,而改变它们不会影响另一个

When you make a copy of a reference type, the new variable refers to the same memory location as the thing you are copying. 制作引用类型的副本时,新变量引用与要复制的内容相同的内存位置。 This means that changing one will change the other since they both refer to the same memory location 这意味着更改一个将更改另一个,因为它们都指向相同的内存位置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值