gpt4 book ai didi

swift - `open` 类的初始化程序是否也需要打开?

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:59 25 4
gpt4 key购买 nike

Swift 3 引入了新的 open我在框架中使用的关键字。

此框架中的 open 类是否需要在所述框架之外使用 open 初始化程序,或者 init 函数是否继承了 open > 上课声明?

例如:

open class OpenClass {
var A: String

init() { // does this init() function need to be marked open?
A = String()
}
}

附带问题:开放类 OpenClass 中的变量是否继承了其类的开放性质?

最佳答案

来自 SE-0117 Allow distinguishing between public access and public overridability :

Initializers do not participate in open checking; they cannot be declared open, and there are no restrictions on providing an initializer that has the same signature as an initializer in the superclass.

您不需要也不能将 init 方法声明为打开:

open class OpenClass {

open init() { // error: only classes and overridable class members can be declared 'open'; use 'public'

}
}

一个类的所有成员的默认访问级别(属性和方法)是内部的,也适用于公开课。

关于swift - `open` 类的初始化程序是否也需要打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39651013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com