1. 程式人生 > >Struct 與Class 的差異性

Struct 與Class 的差異性

原文網址:http://iosdevelopersnote.blogspot.com/2014/12/swift-struct-class.html?utm_source=tuicool&utm_medium=referral

Hi, There。今天這個篇文章要來介紹Swift 中struct 和class 有什麼不一樣的地方?首先要先和大家提到一個觀念,Value Type 和Reference Type 其中struct 是Value Type 而class 是Reference Type 所以這篇文章呈現的struct 的行為也可以套用到所有的value type 物件,相同地class 的行為也可以套用到reference type 的物件上。

我們來建立一個自已的struct 名稱為SRectangle 。程式碼如下呈現。

struct SRectangle {
  var width = 200
}

這個struct 有一個property 名為width 。

再來建立一個class 名為CRectangle 也有一個叫做width 的property 程式碼如下

class  CRectangle { 
 var width = 200
}

準備好了,我們先來看第一個差異點

1. 建構物件時

struct

var sRect = SRectangle()
 //或者

sRect = SRectangle(width: 300 )

sRect.width //結果就是300


class

var cRet = CRectangle()
 // class不能直接用CRectangle(width:300)必需要定義一個constructor 
cRect.width //為200


主要的差別就是class 在產生物件時不能很自然把property 放在constructor 的引數裡

2. 指定給另一個變數的行為不同

struct

var sRect = SRectangle()
 //或者

var sRect2 = sRect

sRect2.width //目前值是200,因為sRect直接copy一份完整記憶體給sRect2 
sRect2.width = 500 
sRect.width // sRect.width值不受sRect2影響還是200


class

var cRect = CRectangle()
 //或者

var cRect2 = cRect

cRect2.width //目前值是200,因為sRect直接copy一份完整記憶體給sRect2 
cRect2.width = 500 
cRect.width // cRect.width也改變成了500


以上是value type 和reference type 最大的不同,在每次做assignment 的時候,value type 都會複製一份完整相同的內容給另一個變數,而class 則是把記憶體的位置給變數。所以reference type 大多會保有一份記憶體而重覆利用。

3. 關於immutable 變數

在Swift 這個語言的特色之一就是可變動內容的變數和不可變動內容的變數用var 和let 來區別。如果一開始用let 來修飾變數就會造成compiler 的錯誤。如下

var name = "Michael" 
name.write( " Pan" ) //這樣是正常

let name = "Michael" 
name.write( " Pan" ) //會造成compile錯誤


struct

大至也遵循這個規則如下

let sRect = SRectangle()

sRect.width = 500  //會造成錯誤


class

let cRect = CRectangle()

cRect.width = 500  //預設可以直接更改雖然是let修飾的cRect


let 這個效果無法延申至class 。不過要提醒大家在Swift 常用的String, Array, Dictionary 都是struct 所以let 是會有效果的

4. mutating function

我們先要為struct 和class 新增一個method 名為changeWidth() 而且我們在不改變原始的程式碼為前提下,新增一個method。如下

struct

struct SRectangle {
 var width = 200  
  
}

extension SRectangle {
  mutating func changeWidth(width:Int){
 self.width = width
 }      
}


class

class  CRectangle { 
  var width = 0
}

extension CRectangle {
  func changeWidth(width:Int){
 self .width = width
 }      
}


這裡用了extension 新增class 裡的method 而不需要改​​到原本的程式碼。以。struct 和class 的差別是struct 的function 要去改變property 的值的時候需要加上mutating 字樣,而class 不用這樣做

5. Struct 和Class 裡的function 共同特色- Implicit External Parameter Name

function 在定義時,有一種特別的寫法叫做external parameter name 舉例來說一般寫法

func setSize( width:Int, height:Int) {
    println( "width \(width), height \(height)" )
}

setSize( 50 , 100 ) //直接丟入引數

External Parameter Name

func setSize(width width:Int, height height:Int) {
  println( "width \(width), height \(height)" )
}

setSize(width: 50 , height: 100 ) //必需在引數值前加上external parameter name

另一個簡寫

func setSize( #width:Int, #height:Int) {
   println( "width \(width), height \(height)" )
}

setSize(width:50, height:100) //當External name和internal name一樣的時候可以在internal name前面加上#就可以少寫external name

這樣的行為對於一般function 並沒有強制一定要寫external name 不過在struct 和class 裡是強制使用的。如下

struct

struct SRectangle {
 var width = 200  
  
}

extension SRectangle {
  mutating func changeWidth(width:Int){
 self.width = width
 }
 func setName(name:String, width:Int) {
 println( "name \(name), width is \(width)" )
 }              
}
var sRect = SRectangle()
sRect.setName( "Michael" , width: 500 )


class

class  CRectangle { 
  var width = 0
}

extension CRectangle {
  func changeWidth(width:Int){
 self .width = width
 }
 func setName(name:String, width:Int) {
 println( "name \(name), width is \(width)" )
 }              
}

var cRect = CRectangle()
cRect.setName( "Michael" , width: 500 )


無論struct 或是class 裡的function 第二個引數開始都強制加入external parameter name。在定義的時候不用特別寫,就會有了。

6. 繼承

物件導向語言,令人覺得強大的能力莫過於繼承,而struct 沒有繼承的功能,只有class 有。如下

class

class CRectangle {
 var width = 0  
}

extension CRectangle {
  func changeWidth(width:Int){
 self.width = width
 }      
}
class CSquare : CRectangle {   // :CRectangle代表繼承的語法
  
  // CSquare這個class看起來什麼內容都沒有
}

var cSquare = CSquare()
cSquare.changeWidth( 300 )   //可以直接使用父類別的function 
cSquare.width   //也可以直接使用從父類別繼承下來的property


以上是簡單常用到的行為裡struct 和class 常令使用者搞混的情況,希望對大家有幫助。歡迎提供更多建議。