本文介绍了Swift中的'open'关键字是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ObjectiveC.swift 文件包含第228行以下几行代码:

The ObjectiveC.swift file from the standard library contains the following few lines of code around line 228:

extension NSObject : Equatable, Hashable {
  /// ...
  open var hashValue: Int {
    return hash
  }
}

在这种情况下open var是什么意思,或者open关键字通常是什么?

What does open var mean in this context, or what is the open keyword in general?

推荐答案

open是Swift 3中的新访问级别,随实现一起引入的

open is a new access level in Swift 3, introduced with the implementationof

自2016年8月7日起,Swift 3快照即可提供该功能,以及Xcode 8 beta 6.

It is available with the Swift 3 snapshot from August 7, 2016,and with Xcode 8 beta 6.

简而言之:

  • open类可在其外部可访问可子类定义模块. open类成员可以访问 ,并且定义模块之外的 overridable .
  • public类可可访问,但不可子类定义模块. public类成员可以访问 ,但是在定义模块之外的不可覆盖.
  • An open class is accessible and subclassable outside of thedefining module. An open class member is accessible andoverridable outside of the defining module.
  • A public class is accessible but not subclassable outside of thedefining module. A public class member is accessible butnot overridable outside of the defining module.

所以openpublic以前的版本Swift版本和public的访问受到限制.或者,就像克里斯·拉特纳(Chris Lattner)所说的那样 SE-0177:允许区分公共访问权限和公共可重写权限:

So open is what public used to be in previousSwift releases and the access of public has been restricted.Or, as Chris Lattner puts it inSE-0177: Allow distinguishing between public access and public overridability:

在您的示例中,open var hashValue是可访问的属性,可以在NSObject子类中覆盖.

In your example, open var hashValue is a property which is accessible and can be overridden in NSObject subclasses.

有关更多示例和详细信息,请参阅.

For more examples and details, have a look at SE-0117.

这篇关于Swift中的'open'关键字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:59