這篇文章主要通過(guò)演示類在不同發(fā)展中的不通過(guò)定義方法,來(lái)向讀者表述它們之間的區(qū)別和聯(lián)系。
在C#1時(shí)代,我們喜歡這樣定義類:
public class Product { private string _name; public string Name { get { return _name; } }
private decimal _price; public decimal Price { get { return _price; } }
public Product(string name, decimal price) { this._name = name; this._price = price; }
public static ArrayList GetArrayList() { ArrayList list = new ArrayList(); list.Add(new Product('WindowsPhone', 10m)); list.Add(new Product('Apple', 10m)); list.Add(new Product('Android', 10m));
return list; }
public override string ToString() { return String.Format('{0}--{1}', _name, _price); } }
局限性: 1、ArrayList沒有提供與其內(nèi)部?jī)?nèi)容有關(guān)的編譯時(shí)信息,如果不慎寫錯(cuò),編譯器也不會(huì)出現(xiàn)任何提示;
2、代碼中為屬性提供了公共的取值方法,這意味著如果添加對(duì)應(yīng)的賦值方法,那么賦值方法必須是共有的;
3、用于創(chuàng)建屬性和變量的代碼很復(fù)雜,這違背了我們進(jìn)行封裝的原本意愿;
在C#2時(shí)代,我們喜歡這樣定義類:
public class Product { private string _name; public string Name { get { return _name; } private set { _name = value; } }
private decimal _price; public decimal Price { get { return _price; } private set { _price = value; } }
public Product(string name, decimal price) { Name = name; Price = price; }
public static List GetArrayList() { List list = new List(); list.Add(new Product('WindowsPhone', 10m)); list.Add(new Product('Apple', 10m)); list.Add(new Product('Android', 10m));
return list; }
public override string ToString() { return String.Format('{0}--{1}', Name, Price); } }
現(xiàn)在,屬性擁有了私有的賦值方法,并且它能非常聰明的“猜出”List是告知編譯器列表中只能包含Product。
試圖將一個(gè)不同類型添加到列表中會(huì)造成編譯時(shí)錯(cuò)誤,并且當(dāng)你從列表中獲取結(jié)果時(shí),也并不需要轉(zhuǎn)化結(jié)果的類型;有效的解決了C#1中的前兩個(gè)問(wèn)題;
在C#3時(shí)代,我們喜歡這樣定義類:
public class Product { public string Name { get; private set; }
public decimal Price { get; private set; }
public Product(){}
public static List GetArrayList() { return new List() { new Product {Name = 'WindowsPhone', Price = 10m}, new Product {Name = 'Apple', Price = 10m}, new Product {Name = 'Android', Price = 10m} }; }
public override string ToString() { return String.Format('{0}--{1}', Name, Price); } }
發(fā)展到這個(gè)階段,我們可以很明顯的發(fā)現(xiàn),不再有任何代碼(或者可見的變量)與屬性關(guān)聯(lián),而且硬編碼的列表是以一種全然不同的方式構(gòu)建,這樣一來(lái),我們實(shí)際上是完全可以刪除就有的構(gòu)造函數(shù),但是外部代碼就不能再創(chuàng)建其他的產(chǎn)品實(shí)例。自動(dòng)熟悉大大簡(jiǎn)化了操作;
在C#4時(shí)代,我們喜歡這樣定義類:
public class Product { private readonly string _name;
public string Name { get { return _name; } }
private readonly decimal _price;
public decimal Price { get { return _price; } }
public Product(string name,decimal price) { this._name = name; this._price = price; }
public static List GetArrayList() { return new List() { new Product(name: 'WindowsPhone', price: 10m), new Product(name: 'Apple', price: 10m), new Product(name: 'Android', price: 10m) }; }
public override string ToString() { return String.Format('{0}--{1}', Name, Price); } }
在這個(gè)特定的示例中,C#4的特性的好處還不是很明顯,但當(dāng)方法或構(gòu)造函數(shù)包含多個(gè)參數(shù)時(shí),它可以是代碼的含義更加清-特別是當(dāng)參數(shù)類型相同,或某個(gè)參數(shù)為null時(shí)。當(dāng)然,你可以選擇什么時(shí)候使用該特性,只在是代碼更好的理解時(shí)才指定參數(shù)的名稱;
總結(jié): C#1(只讀屬性,弱類型集合)------>C#2(私有屬性賦值方法,強(qiáng)類型集合)------>C#3(自動(dòng)實(shí)現(xiàn)的熟悉,增強(qiáng)的集合和對(duì)象初始化)------>C#4(用命名實(shí)參更清晰的調(diào)用構(gòu)造函數(shù)和方法) 注:由于C#5在這方面的特性表現(xiàn)的沒有太大變化,所以就不再表述。
|