abstract:1.抽象什么是面向?qū)ο蟮某橄??這里不說純理論的知識(shí),其實(shí)說白了,就是從我們自身觀察者角度找出類和對(duì)象,構(gòu)建對(duì)象模型。在實(shí)際操作中,我們只需要記住下面幾個(gè)特質(zhì)即可:1. 模塊間的關(guān)聯(lián)強(qiáng)度應(yīng)該是最低的;(低耦合) [重要]2. 模塊內(nèi)各個(gè)元素的關(guān)聯(lián)強(qiáng)度是緊密的;(高內(nèi)聚) [重要]3. 抽象足夠多的特征來進(jìn)行有意義和有效的交互; [了解]4. 類和模塊的接口記錄全部特征; [了解]5. 訪問抽象底層表
1.抽象
什么是面向?qū)ο蟮某橄??這里不說純理論的知識(shí),其實(shí)說白了,就是從我們自身觀察者角度找出類和對(duì)象,構(gòu)建對(duì)象模型。
在實(shí)際操作中,我們只需要記住下面幾個(gè)特質(zhì)即可:
1. 模塊間的關(guān)聯(lián)強(qiáng)度應(yīng)該是最低的;(低耦合) [重要]
2. 模塊內(nèi)各個(gè)元素的關(guān)聯(lián)強(qiáng)度是緊密的;(高內(nèi)聚) [重要]
3. 抽象足夠多的特征來進(jìn)行有意義和有效的交互; [了解]
4. 類和模塊的接口記錄全部特征; [了解]
5. 訪問抽象底層表現(xiàn)形式才能夠有效地實(shí)現(xiàn)操作。 [了解]
2.封裝
我們熟知,C#的class對(duì)象中,主要分為field(字段)、property(屬性)和method(方法)。
如:
public class Person { #region Fields private string _name; private bool? _sex; #endregion #region Constructors public Person(string name) : this(name, null) { } public Person(string name, bool? sex) { this._name = name; this._sex = sex; } #endregion #region Properties public string Name { get { return this._name; } } public bool? Sex { get { return this._sex; } } #endregion #region Methods private string GetSexName(bool? value) { string sexName = null; switch (value) { case null: sexName = "undefined"; break; case true: sexName = "male"; break; case false: sexName = "female"; break; default: break; } return sexName; } public void Speak(string words) { Console.WriteLine(string.Format("{0}({1}) says: {2}", this._name, this.GetSexName(this._sex), words)); } #endregion }
我們需要注意的封裝對(duì)象的修飾符,分別用修飾符public, internal, protected, private設(shè)定,可作用于類的字段,屬性和方法,甚至于類對(duì)象本身。具體的訪問權(quán)限如下:
public: 所有對(duì)象都可以訪問;
protected internal:同一個(gè)程序集內(nèi)的對(duì)象,或者該類對(duì)象以及子類可訪問;
internal:同一個(gè)程序集的對(duì)象可訪問;
protected:該類對(duì)象以及其子類對(duì)象可訪問;
private: 只有該類對(duì)象本身在對(duì)象內(nèi)部可訪問;
對(duì)Person類內(nèi)部代碼的解析(針對(duì)于需掌握基礎(chǔ)知識(shí)的讀者)
#region Your Description Here
#endregion
折疊代碼用,若代碼很長(zhǎng),我們可以用此方式折疊代碼。折疊后,在VS編輯器可以折疊,折疊后只會(huì)看到對(duì)該折疊部分的描述文字,即:Your Description Here
1. field定義
_name和_sex是兩個(gè)字段,并定義為private,即:只能在該類對(duì)象內(nèi)部可訪問;
其中:bool? 這個(gè)是對(duì)bool類型的一個(gè)擴(kuò)展。我們所熟知的bool是一個(gè)值類型,在其后加上"?",即是一個(gè)新的數(shù)據(jù)類型,在bool的基礎(chǔ)上擴(kuò)展為可為null值的數(shù)據(jù)類型(引用類型)。
我們可以從下面的GetSexName函數(shù)略知其常見用法:
private string GetSexName(bool? value) { string sexName = null; if (value == null) { sexName = "undefined"; } else { sexName = value.Value ? "male" : "female"; } return sexName; }
2. constructor定義
定義了兩個(gè)構(gòu)造函數(shù)(constructor)
public Person(string name) : this(name, null) { } public Person(string name, bool? sex) { this._name = name; this._sex = sex; }
我們可以在實(shí)例化的時(shí)候,有兩種選擇。一種是傳一個(gè)參數(shù)name,另一種是傳兩個(gè)參數(shù)name和sex。
若只傳一個(gè)參數(shù),則會(huì)選擇第一個(gè)構(gòu)造函數(shù):
public Person(string name) : this(name, null) { }
其中,該構(gòu)造函數(shù)會(huì)再次調(diào)用兩個(gè)參數(shù)的構(gòu)造函數(shù),即內(nèi)部構(gòu)造函數(shù)間可用上述代碼的方式調(diào)用。若該構(gòu)造函數(shù)內(nèi)部有自身的邏輯,那么,這些邏輯會(huì)在調(diào)用完:this(...)函數(shù)后再執(zhí)行。
3. Property定義
public string Name { get { return this._name; } } public bool? Sex { get { return this._sex; } }
只定義了get,并為定義set
如若想要外部可對(duì)property進(jìn)行賦值,可加上set,例Sex屬性:
public bool? Sex { get { return this._sex; } set { this._sex = value; } }
4. Method定義
定義了兩個(gè)函數(shù),一個(gè)是public void Speak(string words),另一個(gè)是private string GetSexName(bool? value)
這里,我們要新增一個(gè)知識(shí)點(diǎn),即一個(gè)針對(duì)函數(shù)傳參的語(yǔ)法糖,例Speak函數(shù):
public void Speak(string words="Hello, world!") { Console.WriteLine(string.Format("{0}({1}) says: {2}", this._name, this.GetSexName( this._sex), words)); }
細(xì)細(xì)對(duì)照,可發(fā)現(xiàn)在Speak函數(shù)傳參words的地方,有一個(gè)預(yù)定義為"Hello, world!"的值。
上面的代碼即類似于下面的函數(shù)重載效果:
public void Speak() { this.Speak("Hello, world!"); } public void Speak(string words) { Console.WriteLine(string.Format("{0}({1}) says: {2}", this._name, this.GetSexName(this._sex), words)); }
兩種寫法,我們?cè)谡{(diào)用的時(shí)候,都可以傳words參數(shù)的值,也可以不寫,效果是一樣的。但前面的那種寫法更加簡(jiǎn)潔明了,現(xiàn)多被廣泛使用。