-
Notifications
You must be signed in to change notification settings - Fork 15
/
10prototype.go
79 lines (73 loc) · 1.59 KB
/
10prototype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
@date: 2021/5/3
**/
package design
import "fmt"
/**
* @Description: 简历类,里面包含简历的基本信息
*/
type Resume struct {
name string
age int64
sex string
company string
experience string
}
/**
* @Description: 设置简历个人信息
* @receiver r
* @param name
* @param age
* @param sex
*/
func (r *Resume) setPersonInfo(name string, age int64, sex string) {
r.name = name
r.age = age
r.sex = sex
}
/**
* @Description: 设置工作经验
* @receiver r
* @param company
* @param experience
*/
func (r *Resume) setWorkExperience(company string, experience string) {
r.company = company
r.experience = experience
}
/**
* @Description: 显示简历内容
* @receiver r
*/
func (r *Resume) display() {
fmt.Printf("我的名字是%s,性别%s,今年%d岁,在%s工作,工作经验为:%s \n", r.name, r.sex, r.age, r.company, r.experience)
}
/**
* @Description: 深拷贝,原型模式的核心
* @receiver r
* @return *Resume
*/
func (r *Resume) clone() *Resume {
return &Resume{
name: r.name,
sex: r.sex,
age: r.age,
company: r.company,
experience: r.experience,
}
}
func prototypeMain() {
fmt.Println("---------------------------原简历")
resume := &Resume{
name: "王工作",
sex: "男",
age: 10,
company: "光明顶无限责任公司",
experience: "学武功和划水、摸鱼",
}
resume.display()
fmt.Println("---------------------------简历特别好,抄")
copyResume := resume.clone()
copyResume.setPersonInfo("李工作", 21, "男")
copyResume.display()
}