我们来描述一个数据结构,一个公司,下有很多部门,一个部门有很多职员。
ok,我们先实现职员数据结构
我们再定义部门的数据结构,该结构包含了职员的集合
再对StaffCollection依葫芦画瓢,再写一个DepartmentCollection
注意观察DepartmentCollection和StaffCollection的ToArray方法,封装了ToArray复杂性。
最后,我们实现公司的结构
现在我们有了5个类
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff
我们看下具体的应用
继续优化,请看下篇,索引器的重载
ok,我们先实现职员数据结构
1
public class Staff
2
{
3
//默认为男性
4
public Staff(string name):this(name,Sex.Man)
5
{
6
}
7
public Staff(string name,Sex sex)
8
{
9
Name = name;
10
Sex = sex;
11
}
12![]()
13
public readonly string Name;
14
public readonly Sex Sex;
15
}
16![]()
17
public enum Sex
18
{
19
Man,
20
Female
21
}
public class Staff2
{3
//默认为男性4
public Staff(string name):this(name,Sex.Man)5
{6
}7
public Staff(string name,Sex sex)8
{9
Name = name;10
Sex = sex;11
}12

13
public readonly string Name;14
public readonly Sex Sex;15
}16

17
public enum Sex18
{19
Man,20
Female21
}then, 我们再来处理职员的集合
1
public class StaffCollection : System.Collections.CollectionBase
2
{
3![]()
4
public int Add(string name)
5
{
6
return Add(name, Sex.Man);
7
}
8![]()
9
public int Add(string name, Sex sex)
10
{
11
return Add(new Staff(name, sex));
12
}
13
public int Add(Staff staff)
14
{
15
return this.List.Add(staff);
16
}
17
public void AddRange(Staff[] staffs)
18
{
19
for (int i = 0; i <= staffs.Length - 1; i++)
20
{
21
Add(staffs[i]);
22
}
23
}
24![]()
25
public void Remove(Staff staff)
26
{
27
this.List.Remove(staff);
28
}
29![]()
30
public Staff[] ToArray()
31
{
32
Staff[] tmpStaffs = new Staff[this.Count];
33
for (int i = 0; i <= this.Count - 1; i++)
34
{
35
tmpStaffs[i] = (Staff)this[i];
36
}
37
return tmpStaffs;
38
}
39![]()
40
public Staff this[int index]
41
{
42
set
43
{
44
this.List[index] = value;
45
}
46
get
47
{
48
return (Staff)this.List[index];
49
}
50
}
51![]()
52
}
public class StaffCollection : System.Collections.CollectionBase2
{3

4
public int Add(string name)5
{6
return Add(name, Sex.Man);7
}8

9
public int Add(string name, Sex sex)10
{11
return Add(new Staff(name, sex));12
}13
public int Add(Staff staff)14
{15
return this.List.Add(staff);16
}17
public void AddRange(Staff[] staffs)18
{19
for (int i = 0; i <= staffs.Length - 1; i++)20
{21
Add(staffs[i]);22
}23
}24

25
public void Remove(Staff staff)26
{27
this.List.Remove(staff);28
}29

30
public Staff[] ToArray()31
{32
Staff[] tmpStaffs = new Staff[this.Count];33
for (int i = 0; i <= this.Count - 1; i++)34
{35
tmpStaffs[i] = (Staff)this[i];36
}37
return tmpStaffs;38
}39

40
public Staff this[int index]41
{42
set43
{44
this.List[index] = value;45
}46
get47
{48
return (Staff)this.List[index];49
}50
}51

52
}我们再定义部门的数据结构,该结构包含了职员的集合
1
public class Department
2
{
3
public StaffCollection Staffs=new StaffCollection();
4![]()
5
public Department(string name)
6
{
7
Name=name;
8
}
9
public readonly string Name;
10
}
public class Department2
{3
public StaffCollection Staffs=new StaffCollection();4

5
public Department(string name)6
{7
Name=name;8
}9
public readonly string Name;10
}再对StaffCollection依葫芦画瓢,再写一个DepartmentCollection
1
public class DepartmentCollection : System.Collections.CollectionBase
2
{
3
public int Add(string departmentName)
4
{
5
return Add(new Department(departmentName));
6
}
7![]()
8
public int Add(Department department)
9
{
10
return this.List.Add(department);
11
}
12
public void AddRange(Department[] departments)
13
{
14
for (int i = 0; i <= departments.Length - 1; i++)
15
{
16
Add(departments[i]);
17
}
18
}
19![]()
20
public void Remove(Department department)
21
{
22
this.List.Remove(department);
23
}
24![]()
25
public Department[] ToArray()
26
{
27
Department[] tmpDepartments = new Department[this.Count];
28
for (int i = 0; i <= this.Count - 1; i++)
29
{
30
tmpDepartments[i] = (Department)this[i];
31
}
32
return tmpDepartments;
33
}
34![]()
35
public Department this[int index]
36
{
37
set
38
{
39
this.List[index] = value;
40
}
41
get
42
{
43
return (Department)this.List[index];
44
}
45
}
46![]()
47![]()
48
}
public class DepartmentCollection : System.Collections.CollectionBase2
{3
public int Add(string departmentName)4
{5
return Add(new Department(departmentName));6
}7

8
public int Add(Department department)9
{10
return this.List.Add(department);11
}12
public void AddRange(Department[] departments)13
{14
for (int i = 0; i <= departments.Length - 1; i++)15
{16
Add(departments[i]);17
}18
}19

20
public void Remove(Department department)21
{22
this.List.Remove(department);23
}24

25
public Department[] ToArray()26
{27
Department[] tmpDepartments = new Department[this.Count];28
for (int i = 0; i <= this.Count - 1; i++)29
{30
tmpDepartments[i] = (Department)this[i];31
}32
return tmpDepartments;33
}34

35
public Department this[int index]36
{37
set38
{39
this.List[index] = value;40
}41
get42
{43
return (Department)this.List[index];44
}45
}46

47

48
}注意观察DepartmentCollection和StaffCollection的ToArray方法,封装了ToArray复杂性。
最后,我们实现公司的结构
1
public class Company
2
{
3
public DepartmentCollection Departments = new DepartmentCollection();
4
}
public class Company2
{3
public DepartmentCollection Departments = new DepartmentCollection();4
}现在我们有了5个类
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff
我们看下具体的应用
1
public static void Main(string[] args)
2
{
3![]()
4
Company com = new Company();
5
com.Departments.Add("HR");
6
com.Departments.Add("Market");
7
com.Departments.Add("Development");
8![]()
9
com.Departments[0].Staffs.Add("Alice");
10
com.Departments[0].Staffs.Add("Amy");
11
com.Departments[0].Staffs.Add("Ellen");
12
com.Departments[2].Staffs.Add("Albert");
13
com.Departments[2].Staffs.Add("Mark");
14
com.Departments[2].Staffs.Add("Kevin");
15
com.Departments[2].Staffs.Add("Neil");
16![]()
17
for (int i = 0; i <= com.Departments.Count - 1; i++)
18
{
19
System.Console.WriteLine(com.Departments[i].Name);
20
for (int j = 0; j <= com.Departments[i].Staffs.Count - 1; j++)
21
{
22
System.Console.WriteLine("\t{0}{1}", com.Departments[i].Staffs[j].Name, com.Departments[i].Staffs[j].Sex == Sex.Man ? "先生" : "女士");
23
}
24
}
25![]()
26![]()
27
}
public static void Main(string[] args)2
{3

4
Company com = new Company();5
com.Departments.Add("HR");6
com.Departments.Add("Market");7
com.Departments.Add("Development");8

9
com.Departments[0].Staffs.Add("Alice");10
com.Departments[0].Staffs.Add("Amy");11
com.Departments[0].Staffs.Add("Ellen");12
com.Departments[2].Staffs.Add("Albert");13
com.Departments[2].Staffs.Add("Mark");14
com.Departments[2].Staffs.Add("Kevin");15
com.Departments[2].Staffs.Add("Neil");16

17
for (int i = 0; i <= com.Departments.Count - 1; i++)18
{19
System.Console.WriteLine(com.Departments[i].Name);20
for (int j = 0; j <= com.Departments[i].Staffs.Count - 1; j++)21
{22
System.Console.WriteLine("\t{0}{1}", com.Departments[i].Staffs[j].Name, com.Departments[i].Staffs[j].Sex == Sex.Man ? "先生" : "女士");23
}24
}25

26

27
}继续优化,请看下篇,索引器的重载

浙公网安备 33010602011771号