建構函式調用虛函數的進一步探討

來源:互聯網
上載者:User
函數 在blog上看到一篇文章《FxCop 的 ConstructorsShouldNotCallBaseClassVirtualMethods 規範 》原文:http://blog.csdn.net/xamcsdn2/archive/2004/08/11/71766.aspx

頗感興趣,寫了一段代碼研究(估計還稱不上研究,就當探索吧)了一下。

代碼如下。

using System;

namespace test_console
{

??? class baseclass
??? {
??????? public int count = 0;
??????? public baseclass()
??????? {
??????????? Console.WriteLine("Call BaseClass's Constructor Function...");
??????????? Increase("base");
??????? }

??????? public virtual void Increase(string type)
??????? {
??????????? Console.WriteLine(type);
??????????? Console.WriteLine("Call BaseClass's Increase Function...");
??????????? count ++;
??????? }
??? }

??? class subclass:baseclass
??? {
??????? public int subcount = 0;

??????? public subclass()
??????? {
??????????? Console.WriteLine("Call SubClass's Constructor Function...");
??????????? Console.WriteLine(subcount.ToString());
??????????? Increase("sub");
??????? }

??????? public override void Increase(string type)
??????? {
??????????? Console.WriteLine(type);
??????????? Console.WriteLine("Call SubClass's Increase Function...");
??????????? subcount ++;
??????????? Console.WriteLine(subcount.ToString());
??????? }
??????? ///
??????? /// 應用程式的主進入點。
??????? ///
??????? [STAThread]
??????? static void Main(string[] args)
??????? {
??????????? subclass c = new subclass();
??????????? Console.ReadLine();
??????? }
??? }
}


baseclass的建構函式調用了Increase()函數,subclass的建構函式同樣調用了此函數。運行結果如下:

Call BaseClass's Constructor Function...
base
Call SubClass's Increase Function...
1
Call SubClass's Constructor Function...
1
sub
Call SubClass's Increase Function...
2

可以看到SubClass的Increase函數被調用了兩次,我在subclass的increase中加入一個參數來判斷是由哪個建構函式來調用的,結果是先由baseclass調用,再由subclass調用。而且看上去似乎baseclass是先於subclass調用的,但是事實究竟如何呢?我們來看一下subclass的建構函式的IL代碼:

.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
????? // Code Size: 51 byte(s)
????? .maxstack 2
????? L_0000: ldarg.0
????? L_0001: ldc.i4.0
????? L_0002: stfld int32 test_console.subclass::subcount
????? L_0007: ldarg.0
????? L_0008: call instance void test_console.baseclass::.ctor()
????? L_000d: ldstr "Call SubClass\'s Constructor Function..."
????? L_0012: call void [mscorlib]System.Console::WriteLine(string)
????? L_0017: ldarg.0
????? L_0018: ldflda int32 test_console.subclass::subcount
????? L_001d: call instance string int32::ToString()
????? L_0022: call void [mscorlib]System.Console::WriteLine(string)
????? L_0027: ldarg.0
????? L_0028: ldstr "sub"
????? L_002d: callvirt instance void test_console.baseclass::Increase(string)
????? L_0032: ret
}
一目瞭然,L_0008: call instance void test_console.baseclass::.ctor()在subclass的建構函式中先調用了baseclass的建構函式。

?

後記:作為面對對象程式設計者的基本知識,在這裡談論子類建構函式先調用父類建構函式這一點似乎有點畫蛇添足,作為小弟的第一篇作品,希望能給那些初學者一些協助吧。


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。