1 Company com = new Company();
2 com.Departments.Add("HR");
3 com.Departments.Add("Market");
4 com.Departments.Add("Development");
5
6 com.Departments[0].Staffs.Add("Alice");
7 com.Departments[0].Staffs.Add("Amy");
8 com.Departments[0].Staffs.Add("Ellen");
9 com.Departments[2].Staffs.Add("Albert");
10 com.Departments[2].Staffs.Add("Mark");
11 com.Departments[2].Staffs.Add("Kevin");
12 com.Departments[2].Staffs.Add("Neil");
13
14 System.Console.WriteLine(com.Departments[0].Staffs[1].Name);
在循环处理的时候,第14行自然没有什么不好的地方,但如果像14行那样,要直接访问某个部门,用int的索引似乎显得太突兀。2 com.Departments.Add("HR");
3 com.Departments.Add("Market");
4 com.Departments.Add("Development");
5
6 com.Departments[0].Staffs.Add("Alice");
7 com.Departments[0].Staffs.Add("Amy");
8 com.Departments[0].Staffs.Add("Ellen");
9 com.Departments[2].Staffs.Add("Albert");
10 com.Departments[2].Staffs.Add("Mark");
11 com.Departments[2].Staffs.Add("Kevin");
12 com.Departments[2].Staffs.Add("Neil");
13
14 System.Console.WriteLine(com.Departments[0].Staffs[1].Name);
我们优化DepartmentCollection的this索引器,重载this
1 public Department this[int index]
2 {
3 set
4 {
5 this.List[index] = value;
6 }
7 get
8 {
9 return (Department)this.List[index];
10 }
11 }
12
13 public Department this[string name]
14 {
15 get
16 {
17 for (int i = 0; i <= this.List.Count - 1; i++)
18 {
19 if (((Department)this.List[i]).Name == name)
20 {
21 return this[i];
22 }
23 }
24 return null;
25 }
26
27 }
2 {
3 set
4 {
5 this.List[index] = value;
6 }
7 get
8 {
9 return (Department)this.List[index];
10 }
11 }
12
13 public Department this[string name]
14 {
15 get
16 {
17 for (int i = 0; i <= this.List.Count - 1; i++)
18 {
19 if (((Department)this.List[i]).Name == name)
20 {
21 return this[i];
22 }
23 }
24 return null;
25 }
26
27 }
然后,我们继续优化Department
1public 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
11 public Staff this[int index]
12 {
13 set
14 {
15 Staffs[index] = value;
16 }
17 get
18 {
19 return Staffs[index];
20 }
21 }
22 }
加了第11行的this。2 {
3 public StaffCollection Staffs = new StaffCollection();
4
5 public Department(string name)
6 {
7 Name = name;
8 }
9 public readonly string Name;
10
11 public Staff this[int index]
12 {
13 set
14 {
15 Staffs[index] = value;
16 }
17 get
18 {
19 return Staffs[index];
20 }
21 }
22 }
现在的调用代码是
1 Company com = new Company();
2 com.Departments.Add("HR");
3 com.Departments.Add("Market");
4 com.Departments.Add("Development");
5
6 com.Departments[0].Staffs.Add("Alice");
7 com.Departments[0].Staffs.Add("Amy");
8 com.Departments[0].Staffs.Add("Ellen");
9 com.Departments[2].Staffs.Add("Albert");
10 com.Departments[2].Staffs.Add("Mark");
11 com.Departments[2].Staffs.Add("Kevin");
12 com.Departments[2].Staffs.Add("Neil");
13
14 System.Console.WriteLine(com.Departments["Development"][1].Name);
2 com.Departments.Add("HR");
3 com.Departments.Add("Market");
4 com.Departments.Add("Development");
5
6 com.Departments[0].Staffs.Add("Alice");
7 com.Departments[0].Staffs.Add("Amy");
8 com.Departments[0].Staffs.Add("Ellen");
9 com.Departments[2].Staffs.Add("Albert");
10 com.Departments[2].Staffs.Add("Mark");
11 com.Departments[2].Staffs.Add("Kevin");
12 com.Departments[2].Staffs.Add("Neil");
13
14 System.Console.WriteLine(com.Departments["Development"][1].Name);
注意第14行,是不是优雅多了啊?