Further Discussion on the Deficiency Function of Constructor Calling

xiaoxiao2021-03-06  101

See an article on Blog "FXCOP's ConstructorshouseSsthods Specification" original text: http://blog.9cbs.net/xam9cbs2/archive/2004/08/11/71766.aspx

I am very interested, I have written a piece of code. (It seems that I can't study it, I will explore it).

code show as below.

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 ("Call Baseclass's Increase Function ..."); Count ;}} Class Subclass: Baseclass}} Class Subclass: Baseclass {Public Int Subcount = 0; Public Subclass () {Console.WriteLine "Call Subclass's Constructionor Function ..."); console.writeline (Subcount.writeLine (INCRESE ("Sub");} public override void increase (string type) {console.writeline (TYPE); console.writeline "Call Subclass's Increase Function ..."); Subcount ; console.writeline (subcount.tostring ());} ///

/// Application main entry point. /// [stathread] static void main (string [] args) {subclass c = new subclass (); console.writeline (c.count.tostring () "" C. Subcount.toString () Console.readline ();}}}

The BaseClass constructor calls the increase () function, and the SubClass constructor is also called this function. The results of the operation are as follows:

Call BaseClass's Constructor Function ... baseCall SubClass's Increase Function ... 1Call SubClass's Constructor Function ... 1subCall SubClass's Increase Function ... 2 SubClass can see the Increase function is called twice, I joined one of the increase subclass of The parameter is to determine which constructor is called, the result is called by the baseClass, and then calls the subclass. And it seems that Baseclass is called by Subclass, but what is the fact? Let's take a look at the IL code of the Subclass constructor:

.method public hidebysig specialname rtspecialname instance void .ctor () cil managed {// Code Size: 51 byte (s) .maxstack 2L_0000: ldarg.0 L_0001: ldc.i4.0 L_0002: stfld int32 test_console.subclass :: subcountL_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 :: subcountL_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} glance, L_0008: call instance void test_console.baseclass :: ctor () in the constructor of the subclass constructor is called before the baseclass.

Postscript: As the basic knowledge of the object program designer, it seems to have a little painting snake to add, as the first work of the younger, I hope to give those initiators.

转载请注明原文地址:https://www.9cbs.com/read-126001.html

New Post(0)