Swift類與結構體

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   ext   

類和結構體有很多共性:

 

    定義屬性儲存區資料

    定義方法執行功能處理

    定義下標,通過下標訪問他們的值

    初始化他們的狀態

    通過擴充(Extension)擴充其功能

    遵守協議(Protocol),協議提供一種特定的標準

功能

類比結構體多出的功能:


    能夠繼承另外一個類


    能夠核對運行期對象的類型


    析構對象釋放資源


    引用計數允許一個類執行個體有多個引用 

類和結構體的定義 

定義類和結構體: 

class SomeClass {// class definition goes here } struct SomeStructure { // structure definition goes here }

 

執行個體:

struct Resolution { var width= 0 var height =0} class VideoMode { var resolution = Resolution() var interlaced =falsevarframeRate = 0.0 var name: String?}

 

 

類和結構體的執行個體化


struct Resolution { var width= 0 var height =0}class VideoMode { var resolution = Resolution()var interlaced =falsevarframeRate = 0.0var name: String?}let someResolution= Resolution()//let               someResolution                    = Resolution(width:10,height:20)let someVideoMode= VideoMode()

Swift交流討論論壇論壇:http://www.cocoagame.net

歡迎加入Swift技術交流群:362298485




相關文章

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.