class Program
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}
class Tester // 该Tester可帮我们跟踪实例化的过程
{
public Tester(string msg) { Console.WriteLine(msg); }
}
class A
{
static Tester AStatic = new Tester("A static");
Tester AInstance = new Tester("A instance");
public A() { AInstance = new Tester("A constructor"); }
}
class B : A
{
static Tester BStatic = new Tester("B static");
Tester BInstance = new Tester("B instance");
public B() { BInstance = new Tester("B constructor"); }
}
class C : B
{
static Tester CStatic = new Tester("C static");
Tester CInstance = new Tester("C instance");
public C() { CInstance = new Tester("C constructor"); }
}
结果:
C static
C instance
B static
B instance
A static
A instance
A constructor
B constructor
C constructor
Car BWM;
就声明了一个属于类Car的对象BMW。但这个对象的Reference还是null的。即,这个BMW对象只是存在于Steak中,并没有将其指针指向Heap中的真正内容上。
BMW = new Car();
这时,就真正在Heap中实例化了一个对象,这个时候也将Heap上的地址写入了BMW在Steak中的内存地址。BMW有了Reference,且指向了其在Heap中的对象。
1.属性、方法不需要初始化,因为这些全部是指针。
2.初始化派生类的静态字段。
3.初始化派生类的非静态字段。
4.初始化基类的静态字段。
5.初始化基类的非静态字段。
6.调用基类的构造函数。
7.调用派生类的构造函数。
例子如下:
public class a
{
public a()
{
B = 3;
}
public static int staticA = 0;
public int B = 1;
}
public class b : a
{
public b()
{
C = 5;
}
public static int staticB = 2;
public int C = 3;
}