在c#中使用此关键字从另一个构造函数调用一个构造函数若要调用父类中存在的构造函数,请使用base关键字
要调用存在于另一个类中的构造函数,请使用base关键字
class DemoBase{ public DemoBase(int firstNumber, int secondNumber, int thirdNumber){ System.Console.WriteLine("Base class Constructor"); System.Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}"); } } class Demo : DemoBase{ public Demo(int firstNumber, int secondNumber, int thirdNumber) : base(firstNumber, secondNumber, thirdNumber){ System.Console.WriteLine("Derived class Constructor"); System.Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}"); } } class Program{ static void Main(){ Demo obj = new Demo(1, 2, 3); Console.ReadLine(); } }
输出结果
Base class Constructor 1 2 3 Derived class Constructor 1 2 3