Skip to content

Commit 6c0ece2

Browse files
committed
add comment
1 parent 9d22c6d commit 6c0ece2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Prototype/prototype.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (w *WorkExperience) Clone() *WorkExperience {
3434
}
3535
// 淺複製, 複製結構, 但不複製值
3636
shallowObj := w
37-
fmt.Printf("shallowObj: %p,w: %p", &shallowObj, w)
37+
fmt.Printf("shallowObj: %p, orgObj: %p \n", &shallowObj, w)
3838
return shallowObj
3939
}
4040

@@ -84,6 +84,8 @@ func (r *Resume) Display() {
8484
}
8585

8686
// Clone 深複製履歷
87+
// 淺複製: 被複製物件的所有變數都含有與原來的物件相同的值, 而所有的對其物件的參考都能指向
88+
// 深複製: 把參考物件的變數指向複製過的新物件, 而不是原有他被參考的物件
8789
func (r *Resume) Clone() *Resume {
8890
// 深複製
8991
var obj = new(Resume)
@@ -93,9 +95,9 @@ func (r *Resume) Clone() *Resume {
9395
obj.sex = r.sex
9496
return obj
9597

96-
// 淺複製
98+
// 淺複製 : 會導致來源的值也被更改
9799
// shallowObj := r
98-
// shallowObj.name = "kl"
100+
// shallowObj.name = "我改~"
99101
// shallowObj.work = *(r.work).Clone()
100102
// return shallowObj
101103
}

Prototype/prototype_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ func TestPrototype(t *testing.T) {
1111
resume.SetPersonalInfo("男", "24")
1212
resume.SetWorkExperience("2015-2018", "ABC")
1313
resume.Display()
14+
fmt.Println("")
1415

1516
cloneresume := resume.Clone()
1617
cloneresume.SetPersonalInfo("男", "27")
1718
cloneresume.SetWorkExperience("2018-2021", "GBA")
1819
cloneresume.Display()
20+
fmt.Println("")
1921

20-
// 如果是深複製 resume不會被改到 work, 反之淺複製會改到work
22+
// 如果是深複製 原本 resume 物件不會被改到
23+
// 反之淺複製會改到原本的資料
2124
resume.Display()
25+
fmt.Println("")
2226
fmt.Printf("%p ,\t %p \n", &resume, &resume.work)
2327
fmt.Printf("%p ,\t %p \n", &cloneresume, &cloneresume.work)
2428
}

0 commit comments

Comments
 (0)