日期:2014-05-18  浏览次数:21096 次

C#中基类和派生类的构造函数以及变量的执行顺序整理

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 class Program
{
static void Main(string[] args)
{
B b = new B();
Console.WriteLine(A.s_a);
Console.WriteLine(B.s_b);
Console.WriteLine(b.g_a);
Console.WriteLine(b.g_a_1);
Console.WriteLine(b.g_b);
}
}
//基类
class A
{
//静态变量
public static string s_a = "A.s_a" + (C.index++).ToString("_00");
//变量
public string g_a = "A.g_a" + (C.index).ToString("_00");
//为了显示A.g_a
public string g_a_1 = "A.g_a_1" + (C.index++).ToString("_00");

//静态构造函数
static A()
{
Console.WriteLine("It is in static A()" + (C.index++).ToString("_00"));
}
//构造函数
public A()
{
Console.WriteLine("It is in A()" + (C.index++).ToString("_00"));
this.M();
}
//虚方法
public virtual void M()
{
Console.WriteLine("It is in A.M()" + (C.index++).ToString("_00"));
}
}

//派生类
class B : A
{
//静态变量
public static string s_b = "B.s_b" + (C.index++).ToString("_00");
//变量
public string g_b = "B.g_b" + (C.index++).ToString("_00");

//静态构造函数
static B()
{
Console.WriteLine("It is in static B()" + (C.index++).ToString("_00"));
}
//构造函数
public B()
: base()
{
Console.WriteLine("It is in B()" + (C.index++).ToString("_00"));
g_a += "->B.g_a" + (C.index++).ToString("_00");
this.M();
}
//重写方法
public override void M()
{
Console.WriteLine("It is in B.M()" + (C.index++).ToString("_00"));
Console.WriteLine("B.g_a=" + g_a + (C.index++).ToString("_00"));
}
}
class C
{
public static int index = 1;
}

?

代码中使用了另一个类的静态int型变量记录下各个部分的执行顺序。
  运行结果是:

?

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->It is in static B()_02
It is in static A()_05
It is in A()_07
It is in B.M()_08
B.g_a=A.g_a_06_09
It is in B()_10
It is in B.M()_12
B.g_a=A.g_a_06->B.g_a_11_13
A.s_a_04
B.s_b_01
A.g_a_06->B.g_a_11
A.g_a_1_06
B.g_b_03

?

整理下后得到:

?

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->B.s_b_01
It is in static B()_02
B.g_b_03
A.s_a_04
It is in static A()_05
A.g_a_1_06
It is in A()_07
It is in B.M()_08
B.g_a=A.g_a_06_09
It is in B()_10
A.g_a_06->B.g_a_11
It is in B.M()_12
B.g_a=A.g_a_06->B.g_a_11_13

?

 结论
  当实例化一个子类时,父类以及子类的变量和构造函数的执行顺序如下:
01子类的静态变量->02子类的静态构造函数->03子类的变量->04父类的静态变量(此时因为要执行子类的构造函数,会与父类发生关系)
->05父类静态构造函数->06父类变量->07父类构造函数->10子类构造函数->结束

  在试验过程里面添加了几个步骤主要说明子类重写父类方法的调用情况:
1)在父类的构造函数中调用(步骤08),此时调用子类的实现时,其中的变量g_a的值还是来自于父类(步骤09)。
2)在调用子类的构造函数后,变量g_a的值被重新设置(步骤11),此时调用子类的实现时,变量值是子类构造函数中设定的。