Swift - Struct 與 Class 的差異性

4 篇文章 0 订阅

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



原文地址:http://iosdevelopersnote.blogspot.com/2014/12/swift-struct-class.html?utm_source=tuicool&utm_medium=referral
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值