swift 學習(8)Class

Reference types
Struts 是值類型 class 是引用類型
類不會提供自動自動初始化方法,需要自己實現。
類里面的存儲類型必須在初始化方法結束之前完成賦值。

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

struct 的賦值是copy class的賦值是指針指向的。具體見下方代碼。


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

堆和棧區別 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.
系統使用棧來存儲在執行的即時線程上的任何東西;它由CPU嚴格管理和優化。當函數創建一個變量時,堆棧存儲該變量,然后在函數退出時將其銷毀。由于棧是如此良好的組織,它是非常高效的,因此相當快。

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.
系統使用堆來存儲其他對象引用的數據。堆通常是一個大的內存池,系統可以從內存池中請求和動態分配內存塊。堆不會像堆棧那樣自動破壞它的對象;需要額外的工作來做到這一點。相比與堆棧,創建和刪除堆上的數據是一個較慢的過程。

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.

當你創建一個類的實例的時間,會向系統申請在堆上的一塊內存用來存儲類的實例,包括name 和 lastName。同時在棧上存儲內存地址。

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.

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

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

Key points

  • 和structures一樣,類是具有方法和屬性的命名類型(named type)
  • 類通過指針共享對象
  • 類的實例叫做對象(objects)
  • 對象是可變的
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容