你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> swift 中關於open ,public ,fileprivate,private ,internal,修飾的闡明

swift 中關於open ,public ,fileprivate,private ,internal,修飾的闡明

編輯:IOS開發綜合

此篇文章將要引見swift 中關於open ,public ,fileprivate,private ,internal,修飾的闡明的闡明,詳細操作請看面的內容

關於 swift 中的open ,public ,fileprivate,private, internal的區別 以下依照修飾關鍵字的訪問約束范圍 從約束的限定范圍大到小的排序停止闡明

open,public,fileprivate,private,internal 這幾個修飾詞的作用是用於修飾訪問級別的。

、open,public 對應的級別是該模塊或許是援用了該模塊的模塊可以訪問 即 a belong to A , B import A 這兩種狀況都可以對 a停止訪問

、internal 是在模塊外部可以訪問,在模塊內部不可以訪問,a belong A , B import A, A 可以訪問 a, B 不可以訪問a.

、fileprivate 這個修飾跟名字的含義很像,file private 就是文件之間是private的關系,也就是在同一個source文件中還是可以訪問的,但是在其他文件中就不可以訪問了 a belong to file A, a not belong to file B , 在 file A 中 可以訪問 a,在 file B不可以訪問a

、private 這個修飾約束性比fileprivate的約束性更大,private 作用於某個類,也就是說,關於 class A ,假如屬性a是private的,那麼除了A外其他中央都不能訪問了

首先闡明一下這外面的區別在文章開頭英文或鏈接中可以看失掉

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3

Swift provides five differentAccess levelsfor 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 accessandpublic accessenable 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 accessenables 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 accessrestricts 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 accessrestricts 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.

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

Open access applies only to classes and class members, and it differs from public access as follows:

Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.

Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.

Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.

Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.

Marking a class as open explicitly indicates that you’ve considered the impact of code from other modules using that class as a superclass, and that you’ve designed your class’s code accordingly.

Guiding Principle of Access Levels

Access levels in Swift follow an overall guiding principle:No entity can be defined in terms of another entity that has a lower (more restrictive) access level.

For example:

A public variable cannot be defined as having an internal, file-private, or private type, because the type might not be available everywhere that the public variable is used.

A function cannot have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are not available to the surrounding code.

The specific implications of this guiding principle for different aspects of the language are covered in detail below.

多多關注本站,我們將為您搜集更多的Android開發相關文章.

【swift 中關於open ,public ,fileprivate,private ,internal,修飾的闡明】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved