構造函數有很多用處,巧妙地運用構造函數能提高類的設計安全、合理和封裝。
下面的類設計的不合理,要求使用者在調用18行Pring方法前,先為17行指派。
1

public class Space
2
{
3
4
public static void Main(string[] args)
5
6
7
FilePrint pring = new FilePrint();
8
//如果使用者忘記調用FileName,系統将不可靠
9
//pring.FileName = @"c:/text.txt";
10
pring.Print();
11
}
12
13

14

public class FilePrint
15
16
17
public string FileName = null;
18
public void Print()
19
20
System.Console.WriteLine("Do prining {0}", FileName);
21
22
把代碼改為下面就合理的多

FilePrint pring = new FilePrint(@"c:/text.txt");
//代碼可以更簡約
new FilePrint(@"c:/text.txt").Print();


public FilePrint(string path)
FileName = path;
23
public readonly string FileName = null;
24
25
26
27
28
通過構造函數,強制使用者在執行個體化的時候給與FileName的值,調用更安全更簡約。
第23行對資料成員FileName修飾為readonly表示該資料成員為隻讀,隻讀的意思是,該成員隻有一次指派的機會(聲明時立即指派或在構造函數時指派)。readonly和const的差別是
readonly有兩種指派的機會:聲明時立即指派或在構造函數時指派
const隻有在聲明時指派
另外一個很值得注意的是,const其實就是static資料修飾,不過不能直接用static來修飾。const包括了static的含義,是以const需要通過類來通路。
我們再來看一個構造函數的運用
請仔細考慮一下代碼

System.Console.WriteLine(new SalesContract(100).SalePrice);
System.Console.WriteLine(new SalesContract(100,120).SalePrice);
System.Console.WriteLine(new SalesContract(100,120,180).SalePrice);


public class SalesContract : Contract//銷售合同
public SalesContract(double costPrice)
: this(costPrice, costPrice)
{ }
public SalesContract(double costPrice, double minimumPrice)
: this(costPrice, minimumPrice, minimumPrice)
public SalesContract(double costPrice, double minimumPrice, double salePrice)
CostPrice = costPrice;
MinimumPrice = minimumPrice;
29
SalePrice = salePrice;
30
31
32
private double MinimumPrice;//最低價
33
public double SalePrice;//銷售價格
34
35
public bool CheckPrice()//價格檢查
36
37
return SalePrice < Math.Min(MinimumPrice, CostPrice);
38
39
internal double PreferentialPrice;//優惠價
40
41

42

public class Contract//合同
43
44
45
public string Buyer;//買方
46
public string Seller;//賣方
47
protected double CostPrice;//成本價
48
通過3個構造函數,使用者可以友善的對ConstPrice、SalePrice、MinimunPrice進行指派。
我們再來看一個關于構造函數的用例。

System.Console.WriteLine(new Order("123-12").OrderNumber);
System.Console.WriteLine(new Order(Order.CreateOrderNumber()).OrderNumber);


public class Order
public Order(string orderNumber)
this.OrderNumber = orderNumber;
private static int Count;
public readonly string OrderNumber;
public static string CreateOrderNumber()
System.DateTime d = System.DateTime.Now;
return System.DateTime.Now.ToString("yMd-") + (++Count).ToString();
上面的代碼看似很合理,使用者可以自己定義OrderNumber也可以通過CreateOrderNumber方法,Order在構造時總是可以得到Number。
但這樣的設計,暴露了對CreateOrderNumber的調用。
我們看看經過修改後的設計

public Order():this(CreateOrderNumber())
{
private static string CreateOrderNumber()
我們添加了一個預設的構造函數Order()在第9行,又把18行的CreateOrderNumber改為private,那麼這會有什麼效果呢?

System.Console.WriteLine(new Order().OrderNumber);
看到了嗎?在這個代碼中,如果使用者給與了Number則使用使用者給與的值,否則,Order能自動給與值。向使用者封裝了CreateOrderNumber的存在。這就是類設計的一個很重要的原則:自我維護。
本文轉自shyleoking 51CTO部落格,原文連結:http://blog.51cto.com/shyleoking/806286