本文介绍了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:

所以在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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:48
查看更多