微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 为什么我允许方法访问比类访问更少限制?

为什么编译?
internal class A {

    public func f() {

    }
}

我期望f的“公共”修饰符被禁止,因为它的封闭类是内部的.

允许这种情况的一个动机在 SE-0025: Scoped Access Level(强调我的)中提到:

The compiler should not warn when a broader level of access control is used within a type with more restrictive access,such as internal within a private type. This allows the owner of the type to design the access they would use were they to make the type more widely accessible. (The members still cannot be accessed outside the enclosing lexical scope because the type itself is still restricted,i.e. outside code will never encounter a value of that type.)

因此,虽然它不会改变成员的可访问性,但如果封闭类型具有更广泛的访问级别,它允许开发人员传达他们认为给定成员应具有的访问级别 – 例如,这可能对当前具有的API有用.计划在未来版本中公开的内部类型.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐