说明
本文转载自:C#中基类和派生类的构造函数以及变量的执行顺序整理
为方便统一管理自己学习知识体系,所以将博文载入。
源码
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);
Console.ReadKey();
}
}
//基类
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;
}
代码执行结果
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
对执行结果重新排序
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
结论
当实例化一个子类时,父类以及子类的变量初始化和构造函数的执行顺序如下:
1._01子类的静态变量;
2._02子类的静态构造函数;
3._03子类的变量;
4._04父类的静态变量(此时因为要执行子类的构造函数,会与父类发生关系);
5._05父类静态构造函数;
6._06父类变量;
7._07父类构造函数;
8._10子类构造函数。
在试验过程里面添加了几个步骤主要说明子类重写父类方法的调用情况:
1.在父类的构造函数中调用(步骤08),此时调用子类的实现时,其中的变量g_a的值还是来自于父类(步骤09);
2.在调用子类的构造函数后,变量g_a的值被重新设置(步骤11),此时调用子类的实现时,变量值是子类构造函数中设定的。
网友评论