属性和索引器

属性是字段向方法的过渡
1None.gifpublicclassPerson
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifpublicintage;//直接公开字段,无法控制用户输入非法的值
5InBlock.gif
6InBlock.gif
7InBlock.gif//Java模式的对Age控制方式,需要两个方法,Get和Set。麻烦麻烦阿!
8InBlock.gifpublicintGetAge()
9ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
10InBlock.gifreturnAge;
11ExpandedSubBlockEnd.gif}

12InBlock.gif
13InBlock.gifpublicvoidSetAge(intpersonAge)
14ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
15InBlock.gifif(personAge<18)
16ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
17InBlock.gifpersonAge=18;
18ExpandedSubBlockEnd.gif}

19InBlock.gifif(personAge>81)
20ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
21InBlock.gifpersonAge=81;
22ExpandedSubBlockEnd.gif}

23InBlock.gifage=personAge;
24ExpandedSubBlockEnd.gif}

25InBlock.gif
26InBlock.gif//C#模式的Age控制方式,Set和Get是属性的两个访问器,管理方便
27InBlock.gifpublicintAge
28ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
29InBlock.gifset
30ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
31InBlock.gifif(value<18)
32ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
33InBlock.gifvalue=18;
34ExpandedSubBlockEnd.gif}

35InBlock.gifif(value>81)
36ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
37InBlock.gifvalue=81;
38ExpandedSubBlockEnd.gif}

39InBlock.gifage=value;
40ExpandedSubBlockEnd.gif}

41InBlock.gifget
42ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
43InBlock.gifreturnage;
44ExpandedSubBlockEnd.gif}

45InBlock.gif
46ExpandedSubBlockEnd.gif}

47InBlock.gif
48ExpandedBlockEnd.gif}

现在我们来看一个应用,描述部门和员工的一对多的关系。先看一个错误的设计:

1None.gifpublicclassSpace
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifpublicstaticvoidMain(string[]args)
5ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
6InBlock.gifDepartmentdep=newDepartment();
7InBlock.gif//错误一,Staffs所存储的值类型无法被控制。Staffs又可以放字符串,又可以放对象 数据无法统一。
8InBlock.gifdep.Staffs.Add("leo");//放字符串
9InBlock.gifdep.Staffs.Add(newStaff("King"));//放对象
10InBlock.gif
11InBlock.gif//错误二,一旦Department对Staffs的设计改变(比如用数组描述),遍历代码就要改变
//遍历1:类Department将Staffs描述为字符串
12InBlock.giffor(inti=0;i<=dep.Staffs.Count-1;i++)
13ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
14InBlock.gifSystem.Console.WriteLine(dep.Staffs[i]);
15ExpandedSubBlockEnd.gif}

16InBlock.gif
//遍历2:类Department将Staffs描述为数组
17InBlock.giffor(inti=0;i<=dep.Staffs.Length-1;i++)
18ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
19InBlock.gifSystem.Console.WriteLine(dep.Staffs[i]);
20ExpandedSubBlockEnd.gif}

21InBlock.gif
//造成用户必须根据Staffs数据描述的方式不同而编写不同的代码的原因为类Department设计不合理。
22ExpandedSubBlockEnd.gif}

23ExpandedBlockEnd.gif}

24None.gif
25None.gifpublicclassDepartment
26ExpandedBlockStart.gifContractedBlock.gifdot.gif{
27InBlock.gif//这是一个错误的设计,向用户暴露了Staffs的数据结构
28InBlock.gifpublicSystem.Collections.ArrayListStaffs=newSystem.Collections.ArrayList();
29ExpandedBlockEnd.gif}

30None.gif
31None.gifpublicclassDepartment
32ExpandedBlockStart.gifContractedBlock.gifdot.gif{
33InBlock.gif//这是一个错误的设计,向用户暴露了Staffs的数据结构
34ExpandedSubBlockStart.gifContractedSubBlock.gifpublicStaff[]Staffs=newStaff[]dot.gif{newStaff("leo"),newStaff("King")};
35ExpandedBlockEnd.gif}

36None.gif
37None.gifpublicclassStaff
38ExpandedBlockStart.gifContractedBlock.gifdot.gif{
39InBlock.gifpublicStaff(stringname)
40ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
41InBlock.gifName=name;
42ExpandedSubBlockEnd.gif}

43InBlock.gif
44InBlock.gifpublicstringName;
45ExpandedBlockEnd.gif}


好的设计方法是向用户关闭数据结构的细节

1None.gifpublicclassSpace
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifpublicstaticvoidMain(string[]args)
5ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
6InBlock.gifDepartmentdep=newDepartment();
7InBlock.gif//无论将来Staffs的数据结构有什么变化,调用的代码不会有变化
8InBlock.gifdep.AddStaff(newStaff("leo"));
9InBlock.gifdep.AddStaff(newStaff("King"));
10InBlock.giffor(inti=0;i<=dep.StaffsCount-1;i++)
11ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
12InBlock.gifSystem.Console.WriteLine(dep.GetStaffFromIndex(i).Name);
13ExpandedSubBlockEnd.gif}

14ExpandedSubBlockEnd.gif}

15ExpandedBlockEnd.gif}

16None.gif
17None.gifpublicclassDepartment
18ExpandedBlockStart.gifContractedBlock.gifdot.gif{
19InBlock.gif//这是正确的设计
20InBlock.gifprivateSystem.Collections.ArrayListStaffs=newSystem.Collections.ArrayList(); //private隐藏了Staffs的数据结构
21InBlock.gif
22InBlock.gifpublicintStaffsCount //统一遍历代码
23ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
24InBlock.gifget
25ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
26InBlock.gifreturnStaffs.Count;
27ExpandedSubBlockEnd.gif}

28ExpandedSubBlockEnd.gif}

29InBlock.gif
30InBlock.gifpublicintAddStaff(Staffstaff) //统一Staffs的数据结构
31ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
32InBlock.gifreturnthis.Staffs.Add(staff);
33ExpandedSubBlockEnd.gif}

34InBlock.gif
35InBlock.gifpublicvoidRemove(Staffstaff)
36ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
37InBlock.gifthis.Staffs.Remove(staff);
38ExpandedSubBlockEnd.gif}

39InBlock.gif
40InBlock.gifpublicStaffGetStaffFromIndex(intindex)
41ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
42InBlock.gifreturn(Staff)this.Staffs[index];
43ExpandedSubBlockEnd.gif}

44InBlock.gif
45ExpandedBlockEnd.gif}

46None.gif
47None.gifpublicclassStaff
48ExpandedBlockStart.gifContractedBlock.gifdot.gif{
49InBlock.gifpublicStaff(stringname)
50ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
51InBlock.gifName=name;
52ExpandedSubBlockEnd.gif}

53InBlock.gif
54InBlock.gifpublicstringName;
55ExpandedBlockEnd.gif}

如果我们引入索引器,那代码能更合理

1None.gifpublicclassSpace
2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
3InBlock.gif
4InBlock.gifpublicstaticvoidMain(string[]args)
5ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
6InBlock.gifDepartmentdep=newDepartment();
7InBlock.gif//无论将来Staffs的数据结构有什么变化,调用的代码不会有变化
8InBlock.gifdep.AddStaff(newStaff("leo"));
9InBlock.gifdep.AddStaff(newStaff("King"));
10InBlock.giffor(inti=0;i<=dep.StaffsCount-1;i++)
11ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
12InBlock.gifSystem.Console.WriteLine(dep[i].Name);//注意下标
13ExpandedSubBlockEnd.gif}

14ExpandedSubBlockEnd.gif}

15ExpandedBlockEnd.gif}

16None.gif
17None.gifpublicclassDepartment
18ExpandedBlockStart.gifContractedBlock.gifdot.gif{
19InBlock.gif//这是正确的设计
20InBlock.gifprivateSystem.Collections.ArrayListStaffs=newSystem.Collections.ArrayList();
21InBlock.gif
22InBlock.gifpublicintStaffsCount
23ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
24InBlock.gifget
25ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
26InBlock.gifreturnStaffs.Count;
27ExpandedSubBlockEnd.gif}

28ExpandedSubBlockEnd.gif}

29InBlock.gif
30InBlock.gifpublicintAddStaff(Staffstaff)
31ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
32InBlock.gifreturnthis.Staffs.Add(staff);
33ExpandedSubBlockEnd.gif}

34InBlock.gif
35InBlock.gifpublicvoidRemove(Staffstaff)
36ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
37InBlock.gifthis.Staffs.Remove(staff);
38ExpandedSubBlockEnd.gif}

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

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

50ExpandedSubBlockEnd.gif}

51InBlock.gif
52ExpandedBlockEnd.gif}


注意代码的第40的变化,不过在一个类中,只能有一个this[int index]。
注意使用了第40行的索引器,第12行可以象数组下标一样用啦。

不过,这样的设计还不完善,请看下篇,初见继承威力。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值