我們來描述一個資料結構,一個公司,下有很多部門,一個部門有很多職員。
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
then, 我們再來處理職員的集合

public class StaffCollection : System.Collections.CollectionBase
public int Add(string name)
return Add(name, Sex.Man);
public int Add(string name, Sex sex)
return Add(new Staff(name, sex));
public int Add(Staff staff)
return this.List.Add(staff);
public void AddRange(Staff[] staffs)
for (int i = 0; i <= staffs.Length - 1; i++)
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 Department
public StaffCollection Staffs=new StaffCollection();
public Department(string name)
Name=name;
再對StaffCollection依葫蘆畫瓢,再寫一個DepartmentCollection

public class DepartmentCollection : System.Collections.CollectionBase
public int Add(string departmentName)
return Add(new Department(departmentName));
public int Add(Department department)
return this.List.Add(department);
public void AddRange(Department[] departments)
for (int i = 0; i <= departments.Length - 1; i++)
Add(departments[i]);
public void Remove(Department department)
this.List.Remove(department);
public Department[] ToArray()
Department[] tmpDepartments = new Department[this.Count];
tmpDepartments[i] = (Department)this[i];
return tmpDepartments;
public Department this[int index]
return (Department)this.List[index];
注意觀察DepartmentCollection和StaffCollection的ToArray方法,封裝了ToArray複雜性。
最後,我們實作公司的結構

public class Company
public DepartmentCollection Departments = new DepartmentCollection();
現在我們有了5個類
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff
我們看下具體的應用

public static void Main(string[] args)
Company com = new Company();
com.Departments.Add("HR");
com.Departments.Add("Market");
com.Departments.Add("Development");
com.Departments[0].Staffs.Add("Alice");
com.Departments[0].Staffs.Add("Amy");
com.Departments[0].Staffs.Add("Ellen");
com.Departments[2].Staffs.Add("Albert");
com.Departments[2].Staffs.Add("Mark");
com.Departments[2].Staffs.Add("Kevin");
com.Departments[2].Staffs.Add("Neil");
for (int i = 0; i <= com.Departments.Count - 1; i++)
System.Console.WriteLine(com.Departments[i].Name);
for (int j = 0; j <= com.Departments[i].Staffs.Count - 1; j++)
System.Console.WriteLine("/t{0}{1}", com.Departments[i].Staffs[j].Name, com.Departments[i].Staffs[j].Sex == Sex.Man ? "先生" : "女士");
繼續優化,請看下篇,索引器的重載
本文轉自shyleoking 51CTO部落格,原文連結:http://blog.51cto.com/shyleoking/806277