在 C# 中调用基构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/626765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:50:21  来源:igfitidea点击:

Calling base constructor in C#

c#asp.net

提问by DotnetDude

I have the following hierarchy:

我有以下层次结构:

class Base
{
  public Base(string sMessage)
  {
     //Do stuff
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {

   string sMessage = "Blah " + someParams;

   //Here I want to call the base constructor
   //base(sMessage);

  }

}

采纳答案by OregonGhost

You have to call the base class constructor prior to the derived class constructor's body.

您必须在派生类构造函数的主体之前调用基类构造函数。

class Derived : Base
{
  public Derived(string someParams)
    : base("Blah " + someParams)
  {

  }

}

回答by Dinah

You can't. You can call it before:

你不能。你可以在之前调用它:

public Derived() : base()

or you have to use a hook

或者你必须使用钩子

class Base
{
  protected void init() { }
  public Base(string sMessage)
  {
     init();
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {
   string sMessage = "Blah " + someParams;
   init();
  }
}

回答by Garry Shutler

public Derived(string someParams) : base(someParams)
{
    string sMessage = "Blah " + someParams;
}

This is the way you have to do it. You could perhaps put the code you want to call afterwards in a protected method in the base class and then you could call it afterwards like this:

这是你必须做的方式。您也许可以将之后要调用的代码放在基类中的受保护方法中,然后您可以像这样调用它:

class Base
{
  public Base(string sMessage)
  {
     ConstructorStuff();
  }

  protected Base()
  {
  }

  protected void ConstructorStuff()
  {
  }
}

class Derived : Base
{
  public Derived(string someParams)
  {    
   string sMessage = "Blah " + someParams;

   ConstructorStuff();       
  }    
}

回答by Oscar Cabrero

Points to be noted on constructors:

构造函数的注意事项:

· Constructors cannot be "virtual".

· 构造函数不能是“虚拟的”。

· They cannot be inherited.

· 他们不能被继承。

· Constructors are called in the order of inheritance.

· 构造函数按继承顺序调用。

public Child(string a):base(a){}

回答by tvanfosson

If you really need to have your constructor run first, then I suggest using a protected Initialize method that is invoked by your constructors and does the actual work of initializing the class. You need to provide an alternate constructor that will allow the initialization to be skipped.

如果您确实需要先运行构造函数,那么我建议使用受保护的 Initialize 方法,该方法由构造函数调用并执行初始化类的实际工作。您需要提供一个替代构造函数,以允许跳过初始化。

public class Base
{

    public Base() : this(true) { }

    protected Base(bool runInitializer)
    {
        if (runInitializer)
        {
            this.Initialize();
        }
    }

    protected void Initialize()
    {
        ...initialize...
    }
}

public class Derived : Base
{
    // explicitly referencing the base constructor keeps
    // the default one from being invoked.
    public Derived() : base(false)
    {
       ...derived code
       this.Initialize();
    }
}

回答by Tor Haugen

Actually, the simplest solution is:

实际上,最简单的解决方案是:

class Base
{
  public Base(string sMessage)
  {
     //Do stuff
  }
}

class Derived : Base
{
  public Derived(string someParams)
    : base("Blah " + someParams)
  {
  }

}

Why make it more complicated?

为什么让它变得更复杂?

回答by Tor Haugen

I originally missed OregonGhost's comment about using a static method to modify the parameter, which turned out to be the most useful for me, so I thought I'd add a code sample for others who read this thread:

我最初错过了 OregonGhost 关于使用静态方法修改参数的评论,结果证明它对我最有用,所以我想我会为阅读此线程的其他人添加一个代码示例:

class Base
{
    public Base( string sMessage )
    {
        // Do stuff
    }
}

class Derived : Base
{
    public Derived( string sMessage ) : base( AdjustParams( sMessage ) )
    {
    }

    static string AdjustParams( string sMessage )
    {
        return "Blah " + sMessage;
    }
}