使类继承构造函数的最简单方法(C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/958734/
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-06 04:16:08  来源:igfitidea点击:

easiest way to make a class inherit constructors (C#)

c#inheritanceconstructor

提问by

Yeah, sorry about asking a stupid n00b question. So I have a C# program. I have a class

是的,很抱歉问一个愚蠢的 n00b 问题。所以我有一个 C# 程序。我有一堂课

class foo
{

    public int bar, wee, someotherint, someyetanotherint;

    public foo()
    {
         this.bar = 1:
         this.wee = 12;
         this.someotherint = 1231;
         this.someyetanotherint = 443;
    }
}

And I want to make a class called spleen that inherits from foo

我想创建一个继承自 foo 的类,称为脾脏

class spleen : foo
{

}

What is the quickest, easiest syntax to make the class spleen inherit the constructor from foo without having to copy/paste the entire constructor from foo? I don't want to hear that I already have too many parameters. I already know that. (edit: Actually, no. I'm an idiot) I know I should probably somehow call the parent constructor, but I don't know how. How do I do that.

什么是最快,最简单的语法,使类脾从 foo 继承构造函数而不必从 foo 复制/粘贴整个构造函数?我不想听到我已经有太多参数了。我已经知道了。(编辑:实际上,不。我是个白痴)我知道我应该以某种方式调用父构造函数,但我不知道如何调用。我怎么做。

Edit: I realize now that I should have taken more time to write my question. It looks like I was trying to ask two questions at the same time without realizing it (how to inherit constructors with no parameters, and how to inherit constructors with parameters) , and somehow jumbled it all up. However the answers provided were very helpful and solved my problem. Thanks, and sorry for being such an idiot!

编辑:我现在意识到我应该花更多的时间来写我的问题。看起来我试图同时问两个问题而没有意识到它(如何继承没有参数的构造函数,以及如何继承带参数的构造函数),不知何故把它弄乱了。然而,提供的答案非常有帮助,并解决了我的问题。谢谢,对不起,我是个白痴!

采纳答案by JaredPar

What you're looking for is the base keyword. You can use it to call into the base constructor and provide the necessary arguments.

您正在寻找的是 base 关键字。您可以使用它来调用基本构造函数并提供必要的参数。

Try the following. I picked 0 as a default for lack of a better option

请尝试以下操作。由于缺乏更好的选择,我选择了 0 作为默认值

class spleen : foo { 
  public spleen() : base(0,0,0,0)
  {
  }
}

EDIT

编辑

Based on your new version of the code, the simplest way to call the constructor is to quite literally do nothing. The default constructor generated for spleen will automatically call the base empty constructor of foo.

根据您的新版本代码,调用构造函数的最简单方法就是什么都不做。为脾生成的默认构造函数将自动调用 foo 的基空构造函数。

class spleen : foo { 
  public spleen() {}
}

回答by Reed Copsey

You just create it, and call the base constructor:

您只需创建它,然后调用基本构造函数:

public spleen(int bar, int wee, int someotherint, int someyetanotherint)
    : base(bar,wee,someotherint,someyetanotherint)
{
    // Do any spleen specific stuff here
}

回答by Knowles Atchison

Make the constructor protected:

使构造函数受保护:

protected Operator(String s, int i, boolean flag){
    operator = s;
    precedenceLevel = i;
    associative = flag;
}

//stuff

//东西

class MulOperator extends Operator {
protected MulOperator(String s, int precedenceLevel, boolean flag) {
    super(s, precedenceLevel, flag);
}

so on and so forth

等等等等

EDIT: Assuming they have the same/similar constructor...

编辑:假设他们有相同/相似的构造函数......

回答by Kyle Sonaty

JaredPar is right but missed something and I don't have enough rep to edit it.

JaredPar 是对的,但遗漏了一些东西,我没有足够的代表来编辑它。

class spleen : foo 
{
     public spleen() : base()
}

If you parent class took in parameter in the constructor then it would be

如果您的父类在构造函数中接受参数,那么它将是

class foo
{

     public int bar, wee, someotherint, someyetanotherint;

     public foo(int bar, int wee, int someotherint, int someyetanotherint)
     {
           this.bar = bar;
           this.wee = wee;
           this.someotherint = someotherint;
           this.someyetanotherint = someyetanotherint;

     }
}

class spleen : foo 
{
     public spleen(int bar, 
                   int wee, 
                   int someotherint, 
                   int someyetanotherint) : base(bar, 
                                                 wee, 
                                                 someotherint,  
                                                 someyetanotherint)
}