swift 學(xué)習(xí)(8)Class

Reference types
Struts 是值類型 class 是引用類型
類不會(huì)提供自動(dòng)自動(dòng)初始化方法,需要自己實(shí)現(xiàn)。
類里面的存儲(chǔ)類型必須在初始化方法結(jié)束之前完成賦值。

In Swift, a structure is an immutable value. A class, on the other hand, is a mutable reference.
在swift中 Struts 是一個(gè)不可變的值,Class是可變的引用。

struct 的賦值是copy class的賦值是指針指向的。具體見(jiàn)下方代碼。


class Person {
    var name : String
    init(_ name : String) {
        self.name = name
    }
}
var t1 = Person.init("Yunis")
var t2 = t1
print(t1.name)//Yunis
print(t2.name)//Yunis
t1.name = "Change"
print(t1.name)//Change
print(t2.name)//Change


struct Contact {
    var name: String
}
var t3 = Contact.init(name: "Yunis")
var t4 = t3
print(t3.name)//Yunis
print(t4.name)//Yunis
t3.name = "Change"
print(t3.name)//Change
print(t4.name)//Yunis

堆和棧區(qū)別 The heap vs. the stack

The system uses the stack to store anything on the immediate thread of execution; it is tightly managed and optimized by the CPU. When a function creates a variable, the stack stores that variable and then destroys it when the function exits. Since the stack is so well organized, it’s very efficient, and thus quite fast.
系統(tǒng)使用棧來(lái)存儲(chǔ)在執(zhí)行的即時(shí)線程上的任何東西;它由CPU嚴(yán)格管理和優(yōu)化。當(dāng)函數(shù)創(chuàng)建一個(gè)變量時(shí),堆棧存儲(chǔ)該變量,然后在函數(shù)退出時(shí)將其銷毀。由于棧是如此良好的組織,它是非常高效的,因此相當(dāng)快。

The system uses the heap to store data referenced by other objects. The heap is generally a large pool of memory from which the system can request and dynamically allocate blocks of memory. The heap doesn’t automatically destroy its objects like the stack does; additional work is required to do that. This makes creating and removing data on the heap a slower process, compared to on the stack.
系統(tǒng)使用堆來(lái)存儲(chǔ)其他對(duì)象引用的數(shù)據(jù)。堆通常是一個(gè)大的內(nèi)存池,系統(tǒng)可以從內(nèi)存池中請(qǐng)求和動(dòng)態(tài)分配內(nèi)存塊。堆不會(huì)像堆棧那樣自動(dòng)破壞它的對(duì)象;需要額外的工作來(lái)做到這一點(diǎn)。相比與堆棧,創(chuàng)建和刪除堆上的數(shù)據(jù)是一個(gè)較慢的過(guò)程。

class Person {
    var name : String
    var lastName : String
    init(_ name : String,_ lastName : String) {
        self.name = name
        self.lastName = lastName
    }
    var FullName : String {
        return"\(name) \(lastName)"
    }
    
}

var variable = Person.init("Yunis","Last")
14882717990005.jpg

When you create an instance of a class, your code requests a block of memory on the heap to store the instance itself; that’s the first name and last name inside the instance on the right side of the diagram. It stores the address of that memory in your named variable on the stack; that’s the reference stored on the left side of the diagram.

當(dāng)你創(chuàng)建一個(gè)類的實(shí)例的時(shí)間,會(huì)向系統(tǒng)申請(qǐng)?jiān)诙焉系囊粔K內(nèi)存用來(lái)存儲(chǔ)類的實(shí)例,包括name 和 lastName。同時(shí)在棧上存儲(chǔ)內(nèi)存地址。

Object identity


var t1 = Person.init("Yunis","Last")
var t2 = t1
var t3 = Person.init("Yunis","Last")

t1 === t2//true
t1 === t3//true
t2 === t3//true

t2 = t3

t1 === t2//true
t1 === t3//true
t2 === t3//true


t1.FullName == t2.FullName//true
t1.FullName == t3.FullName//true
t2.FullName == t3.FullName//true

Jut as the == operator checks if two values are equal, the === identity operator
compares the memory address of two references.

== 是比較兩個(gè)值是否一致,=== 是比較兩個(gè)引用地址是否一致。

Structures
  • 用于表示值
  • 隱式的值copy
  • 數(shù)據(jù)不可變
  • 內(nèi)存分配速度快(棧)
Classes
  • 用于表示對(duì)象
  • 隱式的對(duì)象共享
  • 數(shù)據(jù)可變
  • 內(nèi)存分配較慢(堆)

Key points

  • 和structures一樣,類是具有方法和屬性的命名類型(named type)
  • 類通過(guò)指針共享對(duì)象
  • 類的實(shí)例叫做對(duì)象(objects)
  • 對(duì)象是可變的
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容