-
Notifications
You must be signed in to change notification settings - Fork 16
/
composite.go
50 lines (43 loc) · 1.01 KB
/
composite.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
package composite
import (
"container/list"
"reflect"
"strconv"
)
//Employee 职员类
type Employee struct {
Name string
Dept string
Salary int
Subordinates *list.List
}
//NewEmployee 实例化职员类
func NewEmployee(name, dept string, salary int) *Employee {
sub := list.New()
return &Employee{
Name: name,
Dept: dept,
Salary: salary,
Subordinates: sub,
}
}
//Add 添加职员的下属
func (e *Employee) Add(emp Employee) {
e.Subordinates.PushBack(emp)
}
//Remove 删除职员的下属
func (e *Employee) Remove(emp Employee) {
for i := e.Subordinates.Front(); i != nil; i = i.Next() {
if reflect.DeepEqual(i.Value, emp) {
e.Subordinates.Remove(i)
}
}
}
//GetSubordinates 获取职员下属列表
func (e *Employee) GetSubordinates() *list.List {
return e.Subordinates
}
//ToString 获取职员的string信息
func (e *Employee) ToString() string {
return "[ Name: " + e.Name + ", dept: " + e.Dept + ", Salary: " + strconv.Itoa(e.Salary) + " ]"
}