Swift中的‘open‘关键字是什么?

Swift中的'open'关键字是Swift 3引入的一个新访问级别,用于区分公共访问和公共可覆盖性。'open'类可以在定义模块之外被访问和子类化,而'open'类成员则可以在外部被访问和覆盖。这不同于'public','public'的类和成员在外部不可被覆盖。'open'关键字主要应用于库和框架的设计,以防止不预期的子类化导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:What is the 'open' keyword in Swift?

The ObjectiveC.swift file from the standard library contains the following few lines of code around line 228: 标准库中的ObjectiveC.swift文件包含第228行的以下几行代码:

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

What does open var mean in this context, or what is the open keyword in general? open var在这个上下文中意味着什么,或者一般来说open关键字是什么?


#1楼

参考:https://stackoom.com/question/2dPv3/Swift中的-open-关键字是什么


#2楼

open is a new access level in Swift 3, introduced with the implementation of open是Swift 3中的一个新的访问级别,随着实现而引入

It is available with the Swift 3 snapshot from August 7, 2016, and with Xcode 8 beta 6. 它适用于2016年8月7日的Swift 3快照,以及Xcode 8 beta 6。

In short: 简而言之:

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

So open is what public used to be in previous Swift releases and the access of public has been restricted. 如此openpublic曾经在之前的Swift版本中所使用的,并且public的访问受到限制。 Or, as Chris Lattner puts it in SE-0177: Allow distinguishing between public access and public overridability : 或者,正如Chris Lattner在SE-0177中所说:允许区分公共访问和公共覆盖

“open” is now simply “more public than public”, providing a very simple and clean model. “开放”现在只是“比公众更公开”,提供了一个非常简单和干净的模型。

In your example, open var hashValue is a property which is accessible and can be overridden in NSObject subclasses. 在您的示例中, open var hashValue是一个可访问的属性,可以在NSObject子类中重写。

For more examples and details, have a look at SE-0117 . 有关更多示例和详细信息,请查看SE-0117


#3楼

Open is an access level, was introduced to impose limitations on class inheritance on Swift. Open是一个访问级别,是为了对Swift上的类继承施加限制而引入的。

This means that the open access level can only be applied to classes and class members . 这意味着开放访问级别只能应用于类和类成员

In Classes 在课堂上

An open class can be subclassed in the module it is defined in and in modules that import the module in which the class is defined. open类可以在它定义的模块中以及在导入定义类的模块的模块中进行子类化。

In Class members 在班级成员

The same applies to class members. 这同样适用于班级成员。 An open method can be overridden by subclasses in the module it is defined in and in modules that import the module in which the method is defined. open方法可以在定义的模块中以及导入定义方法的模块的模块中被子类覆盖。

THE NEED FOR THIS UPDATE 这个更新的需要

Some classes of libraries and frameworks are not designed to be subclassed and doing so may result in unexpected behavior. 某些类的库和框架不是设计为子类的,这样做可能会导致意外行为。 Native Apple library also won't allow overriding the same methods and classes, 原生Apple库也不允许覆盖相同的方法和类,

So after this addition they will apply public and private access levels accordingly. 因此,在此添加之后,他们将相应地应用公共和私人访问级别。

For more details have look at Apple Documentation on Access Control 有关详细信息,请查看有关访问控制的Apple文档


#4楼

Except for subclass, an open class func is also accessible from anywhere in your project. 除子类外,还可以从项目的任何位置访问open class func Say you are defining an open class function in your Util class like this 假设您在Util类中定义了一个开放类函数,就像这样

open class Utils: NSObject {
    open class func printHello() {
        print("Hello from utils")
    }
}

Then in anywhere of your view controllers, you can simply access this method by calling 然后在视图控制器的任何位置,您只需通过调用即可访问此方法

Utils.printHello()

#5楼

Read open as 阅读开放为

open for inheritance in other modules 在其他模块中打开继承

I repeat open for inheritance in other modules. 我在其他模块中重复打开继承。 So an open class is open for subclassing in other modules that include the defining module. 因此,开放类可以在包含定义模块的其他模块中进行子类化。 Open vars and functions are open for overriding in other modules. 打开vars和函数是打开的,以覆盖其他模块。 Its the least restrictive access level. 它是限制最少的访问级别。 It is as good as public access accept that something that is public is closed for inheritance in other modules. 它与公共访问接受一样好,公共的东西在其他模块中被关闭以继承。

From Apple Docs : 来自Apple Docs

Open access applies only to classes and class members, and it differs from public access as follows: 开放访问仅适用于类和类成员,它与公共访问不同,如下所示:

  1. Classes with public access, or any more restrictive access level, can be subclassed only within the module where they're defined. 具有公共访问权限或任何更严格的访问级别的类只能在定义它们的模块中进行子类化。

  2. Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they're defined. 具有公共访问权限或任何更具限制性的访问级别的类成员只能在定义它们的模块中被子类覆盖。

  3. Open classes can be subclassed within the module where they're defined, and within any module that imports the module where they're defined. 开放类可以在定义它们的模块中进行子类化,也可以在导入模块的任何模块中进行子类化。

  4. Open class members can be overridden by subclasses within the module where they're defined, and within any module that imports the module where they're defined. 开放类成员可以由定义它们的模块中的子类覆盖,也可以在导入定义它们的模块的任何模块中覆盖。

#6楼

open只适用于另一个模块,例如:cocoa pods,或单元测试,我们可以继承或覆盖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值