Swift-----存取控制(private、fileprivate、internal、public、open)

來源:互聯網
上載者:User

標籤:idt   swift   app   --   play   images   屬性   代碼   修飾符   

  在swift中,存取修飾詞有五種,分別是:private、fileprivate、internal、public、open。其中fileprivate和open是swift 3 新添加的。由於之前的存取控制符是基於檔案的,不是基於類的。這樣會有問題,故swift 3 增加了兩個修飾符,對原來的private、public進行了細分。

  從高到低的排序如下:

  open > public > interal > fileprivate > private

(1)private:修飾的屬性或者方法只能在當前類中訪問。

(2)fileprivate:修飾的屬性或者方法只能在當前檔案中訪問。如果一個檔案中含有多個類,也是可以訪問的。

(3)internal:預設存取層級,可寫可不寫。

  修飾的屬性和方法在原始碼的整個模組都可以訪問。

  如果是架構或庫代碼,則在整個架構都可以訪問,架構以外是不可以訪問的。

  如果是App代碼,則在整個App內部都是可以訪問的。

(4)public:可以被任何代碼訪問。但其他模組不可以被override和繼承,而在模組內是可以被override和繼承的。

(5)open:可以被任何模組的代碼訪問,包括override和繼承。

 

Swift-----存取控制(private、fileprivate、internal、public、open)

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.