Swift 3:Public和Internal访问修饰符的区别?

Swift 3: The difference between Public and Internal access modifiers?

我在 Swift 3 中阅读了 Apple 关于访问修饰符的参考资料。我在 Whosebug 上也阅读了同样的内容,但我没有像提问者那样得到答案。据我理解正确,有四个级别:

  1. 打开,Public
  2. 内部
  3. 文件私有
  4. 私人

我为自己创建了方案以了解所有这些修饰符之间的区别并上传了 here。如您所见,Public 和内部修饰符之间没有区别。但是它们处于不同的级别。任何想法将不胜感激!

你的图表不正确。

Public A.swiftB.swift 的成员可用于 C.swiftD.swift。唯一的限制是 类 不能被子类化(它们需要 open.

您标记为 public 的任何内容都可以在您的应用内和应用(模块)外使用。 如果您将某些东西标记为 internal 并且只能在您的应用程序(模块)中使用。这在您开发库(框架)时非常有用,您可以使用 internal 来隐藏库结构。

Apple 的 The Swift Programming Language 一书清楚地解释了这些访问修饰符:

“Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.

File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

Private access restricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration

摘自:Apple Inc.“Swift 编程语言 (Swift 3.1)。”电子书。 https://itun.es/gb/jEUH0.l

  • Internal - 这是 swift 中的默认访问说明符。有了这个我们 可以访问同一模块中的数据成员和成员函数 (目标)。

  • Public - 这是您可以访问所有数据成员和成员的地方 在同一模块内和模块外运行。但你不能 subclass 或覆盖模块外。

  • Open - 与 public 相同,唯一的区别是您可以 subclass 或 在模块外覆盖。

  • Fileprivate - 顾名思义,数据成员和成员函数 可在同一文件中访问。

  • Private - 这是你可以访问的范围内的地方 函数体或 class.

•   Public - Can be used from any module but can’t be subclassed outside defining module (target).

•   Internal - This is default access modifier in swift. Can be accessible from the defining module (target) only.

•   Open - Can be used from any module and can be subclassed outside defining module (target).

•   Swift 4+

•   Fileprivate - Fileprivate members and functions are accessible within the same file, within the extension in same file

•   Private - Private members and functions are accessible within the same file, within the extension in same file.

Swift 访问修饰符

取决于 class、函数或 属性 的访问修饰符,它可以被子classed、覆盖、可访问

访问修饰符可适用于 classfield[About]method。尝试访问、subclass 或覆盖它。

  • 通过 class
  • 访问 fieldmethod
  • 继承与开闭原则[About]
    • 扩展,包装器
    • 后继 class(subclass) 访问修饰符应该相同或 restrict 它(除了 private <-> fileprivate).
    • 后继 method(覆盖)访问修饰符应该相同或 expand it

[Java access modifiers]