<分区>
我目前正在尝试构造一个派生自不同对象的对象,但在调用基本构造函数之前,我想进行一些参数验证。
public FuelMotorCycle(string owner) : base(owner)
{
argumentValidation(owner);
}
现在我明白了最初是先调用基本构造函数,有没有办法只在 argumentValidation 方法之后调用它?
标签 c# inheritance
<分区>
我目前正在尝试构造一个派生自不同对象的对象,但在调用基本构造函数之前,我想进行一些参数验证。
public FuelMotorCycle(string owner) : base(owner)
{
argumentValidation(owner);
}
现在我明白了最初是先调用基本构造函数,有没有办法只在 argumentValidation 方法之后调用它?
最佳答案
将首先调用基础构造函数。
这个例子:
class Program
{
static void Main(string[] args)
{
var dc = new DerivedClass();
System.Console.Read();
}
}
class BaseClass
{
public BaseClass(){
System.Console.WriteLine("base");
}
}
class DerivedClass : BaseClass
{
public DerivedClass()
: base()
{
System.Console.WriteLine("derived");
}
}
将输出:
base
derived
关于c# - :base() in a constructor的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624448/