派生类的构造函数调用基类构造函数的前执行 [英] Invoking constructor of derived class execute before constructor of base class

查看:162
本文介绍了派生类的构造函数调用基类构造函数的前执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我原本具有不同值的一些常量(如MAX_SPEED)在每一个派生类。该想法是在基类的一些方法,使用这些值。这时候,我意识到,我不能这样做与常量,所以我创建只读属性的。

Well, originally I had a couple of constants (like MAX_SPEED) with different values in every of the derived classes. The idea was to use those values in some methods of the base class. That's when I realized that I cannot do that with constants, so I created read-only properties.

我需要一种方法来在瞬间分配这些值私有字段实例化,最好在基类。但首先我必须assing在派生类的原始值。由于这些是属性,我无法找到一种方法,同时确定对它们进行初始化,这样做是在派生的构造函数的唯一方法。

I need a method to assign those values to private fields at the moment of the instantiation, preferably in the base class. But first I have to assing the original values in derived classes. Since those are properties, I couldn't find a way to initialize them while defining, so the only way to do that is in the derived constructors.

这是哪里出了问题来自:值其分配给基类私有字段后初始化。我逃脱的解决方案是创建一个的虚拟方法并做分配那里。

That's where the problem comes: values are initialized after their assigning to private fields in the base class. The solution I get away with is to create a virtual method and to do assigning there.

有没有一种方法调用从基本构造派生类,以便从派生的构造函数代码将先调用?

Is there a way to call a base constructor from derived class so that the code from derived constructor will be invoked first?

class BaseClass
{
    public BaseClass()
    {
        System.Console.WriteLine("This should be shown after");
    }
}

class DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
        System.Console.WriteLine("This should be shown first");
    }
}



当然,在这个例子中,将工作的其他方式周围。有没有解决的办法?

Of course in the example it would work the other way around. Is there a solution?

推荐答案

没有。基类构造函数的总是的派生类构造函数体前执行。但是:

No. The base class constructor is always executed before the body of the derived class constructor. However:


  • 在派生类中的任何实例变量初始化被执行的的基类的构造

  • 的基类的构造函数可以执行,可以在派生类中重写虚拟方法。 这是几乎总是尽管是一个坏主意。(各种正常的前提条件是在这一点上无效,你可以观察已没有确定,因为他们会在构造函数体中设置只读变量。例如伊克)

  • Any instance variable initializers in the derived class are executed before the base class constructor
  • The base class constructor can execute virtual methods which can be overridden in the derived class. This is almost always a bad idea though. (All kinds of normal preconditions are invalid at this point. You can observe readonly variables which haven't been set yet because they'll be set in the constructor body, for example. Ick.)

要证明这两种:

using System;

class BaseClass
{
    public BaseClass()
    {
        VirtualMethod();
        Console.WriteLine("BaseClass ctor body");
    }

    public virtual void VirtualMethod()
    {
        Console.WriteLine("BaseClass.VirtualMethod");
    }
}

class DerivedClass : BaseClass
{
    int ignored = ExecuteSomeCode();

    public DerivedClass() : base()
    {
        Console.WriteLine("DerivedClass ctor body");
    }

    static int ExecuteSomeCode()
    {
        Console.WriteLine("Method called from initializer");
        return 5;
    }

    public override void VirtualMethod()
    {
        Console.WriteLine("DerivedClass.VirtualMethod");
    }
}

class Test
{
    static void Main()
    {
        new DerivedClass();
    }
}



输出:

Output:

Method called from initializer
DerivedClass.VirtualMethod
BaseClass ctor body
DerivedClass ctor body

此外,如果你的基类的构造函数的参数,那么你可以在派生类中,以提供一个参数执行一些代码:

Additionally, if your base class constructor takes a parameter, then you can execute some code in the derived class in order to provide an argument:

DerivedClass() : base(SomeStaticMethod())

所有这些都是非常臭,虽然。什么是您的具体情况?

All of these are fairly smelly though. What's your specific situation?

这篇关于派生类的构造函数调用基类构造函数的前执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆