c# - :base() in a constructor的使用

标签 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/

相关文章:

c# - Linq-To-SQL 中的 Hacker News 风格排序算法

c# - 编写 Luhn 算法

c# - 我可以在 C# 4.0 中指定默认的 Color 参数吗?

c++ - 从派生范围调用函数

php - 从 PHP 中的父类访问具有冲突名称的子属性

javascript - 使用局部 View 、JQuery 和引导模式编辑记录

c# - DataGridView 只允许单击 F2 进行编辑

java - 具有抽象类的 JPA 实体继承 - ConstrainViolationException

java - 如何在 Java 中建模 Can-Be-A 关系?

inheritance - 有没有办法使用 MySQL Workbench 对表继承进行建模?