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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.