【转】构造函数的执行序列
任何構造函數(shù)都可以進行配置,以便在執(zhí)行自己的代碼前調(diào)用其它構造函數(shù)。
我們首先看看在創(chuàng)建類的實例時會發(fā)生什么情況。為了實例化派生的類,必須實例化它的基類。而要實例化這個基類,又必須實例化這個基類的基類,這樣一直實例化到System.Object為止。結果是無論使用什么構造函數(shù)實例化一個類,總是要先調(diào)用System.Object.Object()。
如果對一個類使用非默認的構造函數(shù),默認的情況是在其基類上使用匹配于這個構造函數(shù)簽名的構造函數(shù)。如果沒有找到這樣的構造函數(shù),就使用基類的默認構造函數(shù)(根類System.Object總是要使用默認的構造函數(shù),因為這個類沒有非默認的構造函數(shù))。
我們以下面這個小代碼段例子作一些說明:
???? public class MyBaseClass
???? {
???????? public MyBaseClass()
???????? {
???????? }
?
???????? {
???????? }
???? }
?
???? {
???????? public MyDerivedClass()
???????? {
???????? }
?
???????? {
???????? }
?
???????? {
???????? }
???? }
如果以下面的方式實例化MyDerivedClass:
???? MyDerivedClass myObj = new MyDerivedClass();
則發(fā)生下面的一系列事件:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
??? 2.執(zhí)行MyBaseClass.MyBaseClass()構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass()構造函數(shù)。
?
如果使用下面的語句:
MyDerivedClass myObj = new MyDerivedClass(4);
則發(fā)生下面的—系列事件:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
??? 2.執(zhí)行MyBaseClass.MyBaseClass(int i)構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass(int i)構造函數(shù)。
?
如果使用下面的語句:
MyDerivedClass myObj = new MyDerivedClass(4,8);
則發(fā)生下面的—系列事件:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
??? 2.執(zhí)行MyBaseClass.MyBaseClass()構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass(int i, int j)構造函數(shù)。
?
有時需要對發(fā)生的事件進行更多的控制。例如,在上面的實例化范例中,需要有下面的事件序列:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
??? 2.執(zhí)行MyBaseClass.MyBaseClass(int i)構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass(int i, int j)構造函數(shù)。
使用這個序列可以在編寫MyBaseClass(int i)中使用int i參數(shù)的代碼,即MyDerivedClass(int i, int j)構造函數(shù)要做的工作比較少,只需要處理int j參數(shù)(假定int i參數(shù)在兩種情況下有相同的含義)。我們僅需指定在派生類的構造函數(shù)定義中所使用的基類的構造函數(shù),如下所示:
???? public class MyDerivedClass : MyBaseClass
???? {
????????? ……
???????? public MyDerivedClass(int i, int j) : base(i)
???????? {
???????? }
???? }
其中,base關鍵字指定.NET實例化過程,以使用基類中匹配指定簽名的構造函數(shù)。這個例子中使用一個int參數(shù),所以在實例化序列中應調(diào)用MyBaseClass(int i)。
同時我們也可以使用這個關鍵字指定基類構造函數(shù)的字面值。
???? public class MyDerivedClass : MyBaseClass
???? {
???????? public MyDerivedClass() : base(5)
???????? {
???????? }
???????? ……
???? }
這段代碼將執(zhí)行以下序列:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
????2.執(zhí)行MyBaseClass.MyBaseClass(int i)構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass()構造函數(shù)。
?
下面我們介紹另一個關鍵字this。這個關鍵字指定在調(diào)用指定的構造函數(shù)前,.NET實例化過程對當前類使用非默認的構造函數(shù)。
???? public class MyDerivedClass : MyBaseClass
???? {
???????? public MyDerivedClass() : this(5,6)
???????? {
???????? }
???????? ……
???????? public MyDerivedClass(int i, int j) : base(i)
???????? {
???????? }
???? }
這段代碼將執(zhí)行以下序列:
??????? 1.執(zhí)行System.Object.Object()構造函數(shù)。
??? 2.執(zhí)行MyBaseClass.MyBaseClass(int i)構造函數(shù)。
??? 3.執(zhí)行MyDerivedClass.MyDerivedClass(int i,int j)構造函數(shù)。
??? 4.執(zhí)行MyDerivedClass.MyDerivedClass()構造函數(shù)。
?
唯一的限制是使用this或base關鍵字只能指定一個構造函數(shù)。但是如上所示,這并不是一個很嚴厲的限制,按照上述方式仍可以構造相當復雜的執(zhí)行序列。原文URL:http://www.cnblogs.com/Bear-Study-Hard/archive/2006/01/09/313551.html
轉(zhuǎn)載于:https://www.cnblogs.com/jeriffe/articles/1421265.html
總結
以上是生活随笔為你收集整理的【转】构造函数的执行序列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery Attributes 方法
- 下一篇: rs.Open SQL,Conn,adO