Does the initializer of an `open` class need to be open as well?

妖精的绣舞 提交于 2019-12-20 02:34:50

问题


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

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?

For example:

open class OpenClass {
    var A: String

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

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


回答1:


From 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.

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.



来源:https://stackoverflow.com/questions/39651013/does-the-initializer-of-an-open-class-need-to-be-open-as-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!