【问题标题】:The use of :base() in a constructor [duplicate]在构造函数中使用 :base() [重复]
【发布时间】:2013-05-18 12:49:56
【问题描述】:

我目前正在尝试构造一个派生自不同对象的对象,但在调用基本构造函数之前,我想进行一些参数验证。

public FuelMotorCycle(string owner) : base(owner)
{
    argumentValidation(owner);
}

现在我明白了,最初是先调用基本构造函数,有没有办法只能在argumentValidation方法之后调用它?

【问题讨论】:

  • base 构造将首先发生。
  • 您可以随时按 F5 自行查看。
  • 不,你不能轻易做到这一点。 (正如一些人所提到的,有一些偷偷摸摸的把戏。)但这有什么关系呢?如果参数对于基本构造函数无效,它将抛出。如果它们对基构造函数无效但对派生构造函数无效,它将抛出。无论哪种方式,它都会抛出。而且由于导致 ctor 抛出的程序是 buggy,它永远不会发送给客户,那么抛出的时间早或晚又有什么关系呢?

标签: c# inheritance


【解决方案1】:

将首先调用基本构造函数。

这个例子:

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

【讨论】:

    【解决方案2】:

    现在我明白了,最初首先调用基本构造函数, 有没有办法只能在 argumentValidation 方法之后调用它?

    不,至少不是很直接。

    但是您可以使用一个小的解决方法,其中您有static 方法,该方法接受参数,验证它并在它有效时返回它,如果不是则抛出异常:

    private static string argumentValidate(string owner)
    {
        if (/* validation logic for owner */) return owner;
        else throw new YourInvalidArgumentException();
    }
    

    然后让派生类构造函数通过此方法传递参数,然后再将它们传递给base 构造函数:

    public FuelMotorCycle(string owner) : base(argumentValidate(owner))
    {
    }
    

    【讨论】:

      【解决方案3】:

      首先调用基类构造函数,然后执行方法体中的所有内容。

      【讨论】:

        【解决方案4】:

        Base 用于构造函数。派生类构造函数需要从其基类调用构造函数。当默认构造函数不存在时,可以使用基类引用自定义基类构造函数。在派生类构造函数之前调用基类构造函数,但在基类构造函数之前调用派生类构造函数。I也建议你看看contructor execution from msdn

        using System;
        
        public class A // This is the base class.
        {
            public A(int value)
            {
            // Executes some code in the constructor.
            Console.WriteLine("Base constructor A()");
            }
        }
        
        public class B : A // This class derives from the previous class.
        {
            public B(int value)
            : base(value)
            {
            // The base constructor is called first.
            // ... Then this code is executed.
            Console.WriteLine("Derived constructor B()");
            }
        }
        
        class Program
        {
            static void Main()
            {
            // Create a new instance of class A, which is the base class.
            // ... Then create an instance of B, which executes the base constructor.
            A a = new A(0);
            B b = new B(1);
            }
        }
        

        这是输出:

        Base constructor A()
        Base constructor A()
        Derived constructor B()
        

        【讨论】:

          【解决方案5】:

          根据验证方法的不同,您可以执行以下操作:

          public FuelMotorCycle(string owner) 
              : base(argumentValidationReturningValidatedArg(owner))
          {
          }
          

          如果验证函数是静态方法,例如,这应该没问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-02
            • 1970-01-01
            • 2019-02-01
            • 2018-09-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多