C# · 12月 26, 2021

c#-4.0 – 在c#中继承的调用构造函数的顺序

我刚刚读过C#中的继承,我遇到了构造函数,并写过,构造函数按照派生顺序执行.这是什么意思?那个基类的构造函数将被称为first或Derived类.解决方法 一个基类构造函数被称为first.Refer到以下示例 // Demonstrate when constructors are called.using System;// Create a base class.class A { public A() { Console.WriteLine(“Constructing A.”); }}// Create a class derived from A.class B : A { public B() { Console.WriteLine(“Constructing B.”); }}// Create a class derived from B.class C : B { public C() { Console.WriteLine(“Constructing C.”); }}class OrderOfConstruction { static void Main() { C c = new C(); }}The output from this program is shown here:Constructing A.Constructing B.Constructing C.