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

前端之家收集整理的这篇文章主要介绍了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有用.计划在未来版本中公开的内部类型.

原文链接:https://www.f2er.com/swift/319928.html

猜你在找的Swift相关文章