初见继承威力

我们来描述一个数据结构,一个公司,下有很多部门,一个部门有很多职员。

ok,我们先实现职员数据结构
1None.gifpublicclassStaff
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif//默认为男性
4InBlock.gifpublicStaff(stringname):this(name,Sex.Man)
5ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
6ExpandedSubBlockEnd.gif}

7InBlock.gifpublicStaff(stringname,Sexsex)
8ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
9InBlock.gifName=name;
10InBlock.gifSex=sex;
11ExpandedSubBlockEnd.gif}

12InBlock.gif
13InBlock.gifpublicreadonlystringName;
14InBlock.gifpublicreadonlySexSex;
15ExpandedBlockEnd.gif}

16None.gif
17None.gifpublicenumSex
18ExpandedBlockStart.gifContractedBlock.gifdot.gif{
19InBlock.gifMan,
20InBlock.gifFemale
21ExpandedBlockEnd.gif}

then, 我们再来处理职员的集合

1None.gifpublicclassStaffCollection:System.Collections.CollectionBase
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifpublicintAdd(stringname)
5ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
6InBlock.gifreturnAdd(name,Sex.Man);
7ExpandedSubBlockEnd.gif}

8InBlock.gif
9InBlock.gifpublicintAdd(stringname,Sexsex)
10ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
11InBlock.gifreturnAdd(newStaff(name,sex));
12ExpandedSubBlockEnd.gif}

13InBlock.gifpublicintAdd(Staffstaff)
14ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
15InBlock.gifreturnthis.List.Add(staff);
16ExpandedSubBlockEnd.gif}

17InBlock.gifpublicvoidAddRange(Staff[]staffs)
18ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
19InBlock.giffor(inti=0;i<=staffs.Length-1;i++)
20ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
21InBlock.gifAdd(staffs[i]);
22ExpandedSubBlockEnd.gif}

23ExpandedSubBlockEnd.gif}

24InBlock.gif
25InBlock.gifpublicvoidRemove(Staffstaff)
26ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
27InBlock.gifthis.List.Remove(staff);
28ExpandedSubBlockEnd.gif}

29InBlock.gif
30InBlock.gifpublicStaff[]ToArray()
31ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
32InBlock.gifStaff[]tmpStaffs=newStaff[this.Count];
33InBlock.giffor(inti=0;i<=this.Count-1;i++)
34ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
35InBlock.giftmpStaffs[i]=(Staff)this[i];
36ExpandedSubBlockEnd.gif}

37InBlock.gifreturntmpStaffs;
38ExpandedSubBlockEnd.gif}

39InBlock.gif
40InBlock.gifpublicStaffthis[intindex]
41ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
42InBlock.gifset
43ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
44InBlock.gifthis.List[index]=value;
45ExpandedSubBlockEnd.gif}

46InBlock.gifget
47ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
48InBlock.gifreturn(Staff)this.List[index];
49ExpandedSubBlockEnd.gif}

50ExpandedSubBlockEnd.gif}

51InBlock.gif
52ExpandedBlockEnd.gif}


我们再定义部门的数据结构,该结构包含了职员的集合
1None.gifpublicclassDepartment
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gifpublicStaffCollectionStaffs=newStaffCollection();
4InBlock.gif
5InBlock.gifpublicDepartment(stringname)
6ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
7InBlock.gifName=name;
8ExpandedSubBlockEnd.gif}

9InBlock.gifpublicreadonlystringName;
10ExpandedBlockEnd.gif}

再对StaffCollection依葫芦画瓢,再写一个DepartmentCollection
1None.gifpublicclassDepartmentCollection:System.Collections.CollectionBase
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gifpublicintAdd(stringdepartmentName)
4ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
5InBlock.gifreturnAdd(newDepartment(departmentName));
6ExpandedSubBlockEnd.gif}

7InBlock.gif
8InBlock.gifpublicintAdd(Departmentdepartment)
9ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
10InBlock.gifreturnthis.List.Add(department);
11ExpandedSubBlockEnd.gif}

12InBlock.gifpublicvoidAddRange(Department[]departments)
13ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
14InBlock.giffor(inti=0;i<=departments.Length-1;i++)
15ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
16InBlock.gifAdd(departments[i]);
17ExpandedSubBlockEnd.gif}

18ExpandedSubBlockEnd.gif}

19InBlock.gif
20InBlock.gifpublicvoidRemove(Departmentdepartment)
21ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
22InBlock.gifthis.List.Remove(department);
23ExpandedSubBlockEnd.gif}

24InBlock.gif
25InBlock.gifpublicDepartment[]ToArray()
26ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
27InBlock.gifDepartment[]tmpDepartments=newDepartment[this.Count];
28InBlock.giffor(inti=0;i<=this.Count-1;i++)
29ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
30InBlock.giftmpDepartments[i]=(Department)this[i];
31ExpandedSubBlockEnd.gif}

32InBlock.gifreturntmpDepartments;
33ExpandedSubBlockEnd.gif}

34InBlock.gif
35InBlock.gifpublicDepartmentthis[intindex]
36ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
37InBlock.gifset
38ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
39InBlock.gifthis.List[index]=value;
40ExpandedSubBlockEnd.gif}

41InBlock.gifget
42ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
43InBlock.gifreturn(Department)this.List[index];
44ExpandedSubBlockEnd.gif}

45ExpandedSubBlockEnd.gif}

46InBlock.gif
47InBlock.gif
48ExpandedBlockEnd.gif}

注意观察DepartmentCollection和StaffCollection的ToArray方法,封装了ToArray复杂性。

最后,我们实现公司的结构
1None.gifpublicclassCompany
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gifpublicDepartmentCollectionDepartments=newDepartmentCollection();
4ExpandedBlockEnd.gif}


现在我们有了5个类
Company
---DepartmentCollection
------Department
---------StaffCollection
------------Staff

我们看下具体的应用
1None.gifpublicstaticvoidMain(string[]args)
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifCompanycom=newCompany();
5InBlock.gifcom.Departments.Add("HR");
6InBlock.gifcom.Departments.Add("Market");
7InBlock.gifcom.Departments.Add("Development");
8InBlock.gif
9InBlock.gifcom.Departments[0].Staffs.Add("Alice");
10InBlock.gifcom.Departments[0].Staffs.Add("Amy");
11InBlock.gifcom.Departments[0].Staffs.Add("Ellen");
12InBlock.gifcom.Departments[2].Staffs.Add("Albert");
13InBlock.gifcom.Departments[2].Staffs.Add("Mark");
14InBlock.gifcom.Departments[2].Staffs.Add("Kevin");
15InBlock.gifcom.Departments[2].Staffs.Add("Neil");
16InBlock.gif
17InBlock.giffor(inti=0;i<=com.Departments.Count-1;i++)
18ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
19InBlock.gifSystem.Console.WriteLine(com.Departments[i].Name);
20InBlock.giffor(intj=0;j<=com.Departments[i].Staffs.Count-1;j++)
21ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
22InBlock.gifSystem.Console.WriteLine("/t{0}{1}",com.Departments[i].Staffs[j].Name,com.Departments[i].Staffs[j].Sex==Sex.Man?"先生":"女士");
23ExpandedSubBlockEnd.gif}

24ExpandedSubBlockEnd.gif}

25InBlock.gif
26InBlock.gif
27ExpandedBlockEnd.gif}

继续优化,请看下篇,索引器的重载
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值