属性和索引器

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

12InBlock.gif
13InBlock.gif        public void SetAge(int personAge)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            if (personAge < 18)
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                personAge = 18;
18ExpandedSubBlockEnd.gif            }

19InBlock.gif            if (personAge>81)
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                personAge = 81;
22ExpandedSubBlockEnd.gif            }

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

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

35InBlock.gif                if (value > 81)
36ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
37InBlock.gif                    value = 81;
38ExpandedSubBlockEnd.gif                }

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

41InBlock.gif            get
42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
43InBlock.gif                return age;
44ExpandedSubBlockEnd.gif            }

45InBlock.gif        
46ExpandedSubBlockEnd.gif        }

47InBlock.gif    
48ExpandedBlockEnd.gif    }

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

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

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

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

23ExpandedBlockEnd.gif    }

24 None.gif
25 None.gif     public   class  Department
26 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
27InBlock.gif        //这是一个错误的设计,向用户暴露了Staffs的数据结构
28InBlock.gif        public System.Collections.ArrayList Staffs = new System.Collections.ArrayList();
29ExpandedBlockEnd.gif    }

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

36 None.gif
37 None.gif     public   class  Staff
38 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
39InBlock.gif        public Staff(string name)
40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
41InBlock.gif            Name = name;
42ExpandedSubBlockEnd.gif        }

43InBlock.gif
44InBlock.gif        public string Name;
45ExpandedBlockEnd.gif    }


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

 1 None.gif      public   class  Space
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif
 4InBlock.gif        public static void Main(string[] args)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 6InBlock.gif            Department dep = new Department();
 7InBlock.gif            //无论将来Staffs的数据结构有什么变化,调用的代码不会有变化
 8InBlock.gif            dep.AddStaff(new Staff("leo"));
 9InBlock.gif            dep.AddStaff(new Staff("King"));
10InBlock.gif            for (int i = 0; i <= dep.StaffsCount - 1;i++ )
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                System.Console.WriteLine(dep.GetStaffFromIndex(i).Name);
13ExpandedSubBlockEnd.gif            }

14ExpandedSubBlockEnd.gif        }

15ExpandedBlockEnd.gif    }

16 None.gif
17 None.gif     public   class  Department
18 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
19InBlock.gif        //这是正确的设计
20InBlock.gif        private System.Collections.ArrayList Staffs = new System.Collections.ArrayList();   //private隐藏了Staffs的数据结构
21InBlock.gif
22InBlock.gif        public int StaffsCount                //统一遍历代码
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            get
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                return Staffs.Count;
27ExpandedSubBlockEnd.gif            }

28ExpandedSubBlockEnd.gif        }

29InBlock.gif
30InBlock.gif        public int AddStaff(Staff staff)                //统一Staffs的数据结构
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            return this.Staffs.Add(staff);
33ExpandedSubBlockEnd.gif        }

34InBlock.gif
35InBlock.gif        public void Remove(Staff staff)
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            this.Staffs.Remove(staff);                                                      
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        public Staff GetStaffFromIndex(int index)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            return (Staff)this.Staffs[index];
43ExpandedSubBlockEnd.gif        }

44InBlock.gif
45ExpandedBlockEnd.gif    }

46 None.gif
47 None.gif     public   class  Staff
48 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
49InBlock.gif        public Staff(string name)
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Name = name;
52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif        public string Name;
55ExpandedBlockEnd.gif    }

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

 1 None.gif      public   class  Space
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif
 4InBlock.gif        public static void Main(string[] args)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 6InBlock.gif            Department dep = new Department();
 7InBlock.gif            //无论将来Staffs的数据结构有什么变化,调用的代码不会有变化
 8InBlock.gif            dep.AddStaff(new Staff("leo"));
 9InBlock.gif            dep.AddStaff(new Staff("King"));
10InBlock.gif            for (int i = 0; i <= dep.StaffsCount - 1;i++ )
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                System.Console.WriteLine(dep[i].Name);          //注意下标    
13ExpandedSubBlockEnd.gif            }

14ExpandedSubBlockEnd.gif        }

15ExpandedBlockEnd.gif    }

16 None.gif
17 None.gif     public   class  Department
18 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
19InBlock.gif        //这是正确的设计
20InBlock.gif        private System.Collections.ArrayList Staffs = new System.Collections.ArrayList();
21InBlock.gif
22InBlock.gif        public int StaffsCount
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            get
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                return Staffs.Count;
27ExpandedSubBlockEnd.gif            }

28ExpandedSubBlockEnd.gif        }

29InBlock.gif
30InBlock.gif        public int AddStaff(Staff staff)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            return this.Staffs.Add(staff);
33ExpandedSubBlockEnd.gif        }

34InBlock.gif
35InBlock.gif        public void Remove(Staff staff)
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            this.Staffs.Remove(staff);                                                      
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        public Staff this[int index]
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            set
43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
44InBlock.gif                Staff[index] = value;
45ExpandedSubBlockEnd.gif            }

46InBlock.gif            get
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48InBlock.gif                return (Staff)Staff[index];
49ExpandedSubBlockEnd.gif            }

50ExpandedSubBlockEnd.gif        }

51InBlock.gif
52ExpandedBlockEnd.gif    }


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

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

转载于:https://www.cnblogs.com/shyleoking/archive/2007/02/12/648739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值