属性是字段向方法的过渡
1

public class Person
2
{
3
4
public int age;//直接公开字段,无法控制用户输入非法的值
5
6
7
//Java模式的对Age控制方式,需要两个方法,Get和Set。麻烦麻烦阿!
8
public int GetAge()
9
10
return Age;
11
}
12
13
public void SetAge(int personAge)
14
15
if (personAge < 18)
16
17
personAge = 18;
18
19
if (personAge>81)
20
21
personAge = 81;
22
23
age = personAge;
24
25
26
//C#模式的Age控制方式,Set和Get是属性的两个访问器,管理方便
27
public int Age
28
29
set
30
31
if (value < 18)
32
33
value = 18;
34
35
if (value > 81)
36
37
value = 81;
38
39
age = value;
40
41
get
42
43
return age;
44
45
46
47
48
现在我们来看一个应用,描述部门和员工的一对多的关系。先看一个错误的设计:

public class Space
public static void Main(string[] args)
Department dep = new Department();
//错误一,Staffs所存储的值类型无法被控制。Staffs又可以放字符串,又可以放对象 数据无法统一。
dep.Staffs.Add("leo"); //放字符串
dep.Staffs.Add(new Staff("King")); //放对象
//错误二,一旦Department对Staffs的设计改变(比如用数组描述),遍历代码就要改变
//遍历1:类Department将Staffs描述为字符串
for (int i = 0; i <= dep.Staffs.Count - 1; i++)
System.Console.WriteLine(dep.Staffs[i]);
//遍历2:类Department将Staffs描述为数组
for (int i = 0; i <= dep.Staffs.Length - 1; i++)
//造成用户必须根据Staffs数据描述的方式不同而编写不同的代码的原因为类Department设计不合理。


public class Department
//这是一个错误的设计,向用户暴露了Staffs的数据结构
public System.Collections.ArrayList Staffs = new System.Collections.ArrayList();


public Staff[] Staffs = new Staff[] { new Staff("leo"), new Staff("King") };


public class Staff
public Staff(string name)
Name = name;
public string Name;
好的设计方法是向用户关闭数据结构的细节

//无论将来Staffs的数据结构有什么变化,调用的代码不会有变化
dep.AddStaff(new Staff("leo"));
dep.AddStaff(new Staff("King"));
for (int i = 0; i <= dep.StaffsCount - 1;i++ )
System.Console.WriteLine(dep.GetStaffFromIndex(i).Name);


//这是正确的设计
private System.Collections.ArrayList Staffs = new System.Collections.ArrayList(); //private隐藏了Staffs的数据结构
public int StaffsCount //统一遍历代码
return Staffs.Count;
public int AddStaff(Staff staff) //统一Staffs的数据结构
return this.Staffs.Add(staff);
public void Remove(Staff staff)
this.Staffs.Remove(staff);
public Staff GetStaffFromIndex(int index)
return (Staff)this.Staffs[index];


49
50
51
52
53
54
55
如果我们引入索引器,那代码能更合理

System.Console.WriteLine(dep[i].Name); //注意下标


private System.Collections.ArrayList Staffs = new System.Collections.ArrayList();
public int StaffsCount
public int AddStaff(Staff staff)
public Staff this[int index]
Staff[index] = value;
return (Staff)Staff[index];
注意代码的第40的变化,不过在一个类中,只能有一个this[int index]。
注意使用了第40行的索引器,第12行可以象数组下标一样用啦。
不过,这样的设计还不完善,请看下篇,初见继承威力。
本文转自shyleoking 51CTO博客,原文链接:http://blog.51cto.com/shyleoking/806278