我们来描述一个数据结构,一个公司,下有很多部门,一个部门有很多职员。
ok,我们先实现职员数据结构
我们再定义部门的数据结构,该结构包含了职员的集合
再对StaffCollection依葫芦画瓢,再写一个DepartmentCollection
注意观察DepartmentCollection和StaffCollection的ToArray方法,封装了ToArray复杂性。
最后,我们实现公司的结构
现在我们有了5个类
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff
我们看下具体的应用
继续优化,请看下篇,索引器的重载
ok,我们先实现职员数据结构
1
publicclassStaff
2

{
3
//默认为男性
4
publicStaff(stringname):this(name,Sex.Man)
5

{
6
}
7
publicStaff(stringname,Sexsex)
8

{
9
Name=name;
10
Sex=sex;
11
}
12
13
publicreadonlystringName;
14
publicreadonlySexSex;
15
}
16
17
publicenumSex
18

{
19
Man,
20
Female
21
}
publicclassStaff2


{3
//默认为男性4
publicStaff(stringname):this(name,Sex.Man)5


{6
}7
publicStaff(stringname,Sexsex)8


{9
Name=name;10
Sex=sex;11
}12

13
publicreadonlystringName;14
publicreadonlySexSex;15
}16

17
publicenumSex18


{19
Man,20
Female21
}
then, 我们再来处理职员的集合
1
publicclassStaffCollection:System.Collections.CollectionBase
2

{
3
4
publicintAdd(stringname)
5

{
6
returnAdd(name,Sex.Man);
7
}
8
9
publicintAdd(stringname,Sexsex)
10

{
11
returnAdd(newStaff(name,sex));
12
}
13
publicintAdd(Staffstaff)
14

{
15
returnthis.List.Add(staff);
16
}
17
publicvoidAddRange(Staff[]staffs)
18

{
19
for(inti=0;i<=staffs.Length-1;i++)
20

{
21
Add(staffs[i]);
22
}
23
}
24
25
publicvoidRemove(Staffstaff)
26

{
27
this.List.Remove(staff);
28
}
29
30
publicStaff[]ToArray()
31

{
32
Staff[]tmpStaffs=newStaff[this.Count];
33
for(inti=0;i<=this.Count-1;i++)
34

{
35
tmpStaffs[i]=(Staff)this[i];
36
}
37
returntmpStaffs;
38
}
39
40
publicStaffthis[intindex]
41

{
42
set
43

{
44
this.List[index]=value;
45
}
46
get
47

{
48
return(Staff)this.List[index];
49
}
50
}
51
52
}
publicclassStaffCollection:System.Collections.CollectionBase2


{3

4
publicintAdd(stringname)5


{6
returnAdd(name,Sex.Man);7
}8

9
publicintAdd(stringname,Sexsex)10


{11
returnAdd(newStaff(name,sex));12
}13
publicintAdd(Staffstaff)14


{15
returnthis.List.Add(staff);16
}17
publicvoidAddRange(Staff[]staffs)18


{19
for(inti=0;i<=staffs.Length-1;i++)20


{21
Add(staffs[i]);22
}23
}24

25
publicvoidRemove(Staffstaff)26


{27
this.List.Remove(staff);28
}29

30
publicStaff[]ToArray()31


{32
Staff[]tmpStaffs=newStaff[this.Count];33
for(inti=0;i<=this.Count-1;i++)34


{35
tmpStaffs[i]=(Staff)this[i];36
}37
returntmpStaffs;38
}39

40
publicStaffthis[intindex]41


{42
set43


{44
this.List[index]=value;45
}46
get47


{48
return(Staff)this.List[index];49
}50
}51

52
}
我们再定义部门的数据结构,该结构包含了职员的集合
1
publicclassDepartment
2

{
3
publicStaffCollectionStaffs=newStaffCollection();
4
5
publicDepartment(stringname)
6

{
7
Name=name;
8
}
9
publicreadonlystringName;
10
}
publicclassDepartment2


{3
publicStaffCollectionStaffs=newStaffCollection();4

5
publicDepartment(stringname)6


{7
Name=name;8
}9
publicreadonlystringName;10
}
再对StaffCollection依葫芦画瓢,再写一个DepartmentCollection
1
publicclassDepartmentCollection:System.Collections.CollectionBase
2

{
3
publicintAdd(stringdepartmentName)
4

{
5
returnAdd(newDepartment(departmentName));
6
}
7
8
publicintAdd(Departmentdepartment)
9

{
10
returnthis.List.Add(department);
11
}
12
publicvoidAddRange(Department[]departments)
13

{
14
for(inti=0;i<=departments.Length-1;i++)
15

{
16
Add(departments[i]);
17
}
18
}
19
20
publicvoidRemove(Departmentdepartment)
21

{
22
this.List.Remove(department);
23
}
24
25
publicDepartment[]ToArray()
26

{
27
Department[]tmpDepartments=newDepartment[this.Count];
28
for(inti=0;i<=this.Count-1;i++)
29

{
30
tmpDepartments[i]=(Department)this[i];
31
}
32
returntmpDepartments;
33
}
34
35
publicDepartmentthis[intindex]
36

{
37
set
38

{
39
this.List[index]=value;
40
}
41
get
42

{
43
return(Department)this.List[index];
44
}
45
}
46
47
48
}
publicclassDepartmentCollection:System.Collections.CollectionBase2


{3
publicintAdd(stringdepartmentName)4


{5
returnAdd(newDepartment(departmentName));6
}7

8
publicintAdd(Departmentdepartment)9


{10
returnthis.List.Add(department);11
}12
publicvoidAddRange(Department[]departments)13


{14
for(inti=0;i<=departments.Length-1;i++)15


{16
Add(departments[i]);17
}18
}19

20
publicvoidRemove(Departmentdepartment)21


{22
this.List.Remove(department);23
}24

25
publicDepartment[]ToArray()26


{27
Department[]tmpDepartments=newDepartment[this.Count];28
for(inti=0;i<=this.Count-1;i++)29


{30
tmpDepartments[i]=(Department)this[i];31
}32
returntmpDepartments;33
}34

35
publicDepartmentthis[intindex]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
publicclassCompany
2

{
3
publicDepartmentCollectionDepartments=newDepartmentCollection();
4
}
publicclassCompany2


{3
publicDepartmentCollectionDepartments=newDepartmentCollection();4
}
现在我们有了5个类
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff
我们看下具体的应用
1
publicstaticvoidMain(string[]args)
2

{
3
4
Companycom=newCompany();
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(inti=0;i<=com.Departments.Count-1;i++)
18

{
19
System.Console.WriteLine(com.Departments[i].Name);
20
for(intj=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
}
publicstaticvoidMain(string[]args)2


{3

4
Companycom=newCompany();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(inti=0;i<=com.Departments.Count-1;i++)18


{19
System.Console.WriteLine(com.Departments[i].Name);20
for(intj=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
}
继续优化,请看下篇,索引器的重载
4万+

被折叠的 条评论
为什么被折叠?



