open类的初始化程序是否也需要打开? [英] Does the initializer of an `open` class need to be open as well?

查看:79
本文介绍了open类的初始化程序是否也需要打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Swift 3 introduced the new open keyword that I'm using in a framework.

此框架中的open类是否需要在该框架之外使用open初始化程序,还是init函数继承该类的open声明?

Does an open class in this framework require an open initialiser to be used outside of said framework, or does the init function inherit the open declaration on the class?

例如:

open class OpenClass {
    var A: String

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

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

Side question: do the variables in the open class OpenClass inherit the open nature of their class?

推荐答案

来自

初始化程序不参与公开检查;它们不能被声明为打开状态,并且提供与该超类中的初始化程序具有相同签名的初始化程序没有任何限制.

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:

You need not and you cannot declare a init method as open:

open class OpenClass {

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

    }
}

类的所有成员的默认访问级别(属性 和方法)是内部的 .

The default access level for all members of a class (properties and methods) is internal, that applies to open classes as well.

这篇关于open类的初始化程序是否也需要打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆