C#-构造函数中base

时间:2023-03-24 08:13:26

base
 
是调用基类的有参数构造函数
 

因为在子类不能直接继承父类的构造函数
实例
 using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
public class BaseFather
{
public BaseFather()
{
Console.WriteLine("this is not arguments in base father constructor");
}
public BaseFather(string arg)
{
Console.WriteLine("there is one argument in base father constructor");
}
}
public class Base : BaseFather
{
public Base() : base() // 继承无参构造函数
{
Console.WriteLine("no arguments in base constructor");
}
public Base(string a) : base(a) // 继承有参构造函数
{
Console.WriteLine("there is one argument in base constructor");
}
static void Main(string[] args)
{
Base B1 = new Base();
Base B2 = new Base("s");
Console.ReadKey();
}
}
}

结果

C#-构造函数中baseC#-构造函数中base