構(gòu)造函數(shù)調(diào)用虛函數(shù)的進(jìn)一步探討
發(fā)表時(shí)間:2023-08-17 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在blog上看到一篇文章《FxCop 的 ConstructorsShouldNotCallBaseClassVirtualMethods 規(guī)范 》原文:http://blog.csdn.net/x...
在blog上看到一篇文章《FxCop 的 ConstructorsShouldNotCallBaseClassVirtualMethods 規(guī)范 》原文:http://blog.csdn.net/xamcsdn2/archive/2004/08/11/71766.aspx
頗感興趣,寫(xiě)了一段代碼研究(估計(jì)還稱不上研究,就當(dāng)探索吧)了一下。
代碼如下。
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());
??????? }
??????? ///
??????? /// 應(yīng)用程序的主入口點(diǎn)。
??????? ///
??????? [STAThread]
??????? static void Main(string[] args)
??????? {
??????????? subclass c = new subclass();
??????????? Console.ReadLine();
??????? }
??? }
}
baseclass的構(gòu)造函數(shù)調(diào)用了Increase()函數(shù),subclass的構(gòu)造函數(shù)同樣調(diào)用了此函數(shù)。運(yùn)行結(jié)果如下:
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函數(shù)被調(diào)用了兩次,我在subclass的increase中加入一個(gè)參數(shù)來(lái)判斷是由哪個(gè)構(gòu)造函數(shù)來(lái)調(diào)用的,結(jié)果是先由baseclass調(diào)用,再由subclass調(diào)用。而且看上去似乎baseclass是先于subclass調(diào)用的,但是事實(shí)究竟如何呢?我們來(lái)看一下subclass的構(gòu)造函數(shù)的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的構(gòu)造函數(shù)中先調(diào)用了baseclass的構(gòu)造函數(shù)。
?
后記:作為面對(duì)對(duì)象程序設(shè)計(jì)者的基本知識(shí),在這里談?wù)撟宇悩?gòu)造函數(shù)先調(diào)用父類構(gòu)造函數(shù)這一點(diǎn)似乎有點(diǎn)畫(huà)蛇添足,作為小弟的第一篇作品,希望能給那些初學(xué)者一些幫助吧。