【问题标题】:easiest way to make a class inherit constructors (C#)使类继承构造函数的最简单方法(C#)
【发布时间】:2009-06-06 01:13:03
【问题描述】:

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

class foo
{

    public int bar, wee, someotherint, someyetanotherint;

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

我想创建一个继承自 foo 的名为 spleen 的类

class spleen : foo
{

}

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

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

【问题讨论】:

  • 听起来你可能对措辞感到困惑......你不是从 Spleen 继承的,你是从 Foo 继承的——这使得 Foo 成为基类,Spleen 成为子类
  • 另请注意,您问题中的 'foo' 构造函数当前没有任何参数。无参数构造函数和它们之间的链接,您可以在 C# 中“免费”获得,因此正如您在问题中所写,“脾脏”完全符合您的要求。
  • 为什么说“参数太多”?编辑后,您根本没有任何参数。 (此时,您无需对脾进行任何操作。您显示的代码将编译正常并按预期工作。)
  • 顺便说一句,.NET 约定是以 PascalCase 命名类,因此如果您希望您的代码与其他代码相匹配,您可能需要大写您的类名。

标签: c# inheritance constructor


【解决方案1】:

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

试试下面的。由于没有更好的选择,我选择了 0 作为默认值

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

编辑

根据您的新代码版本,调用构造函数的最简单方法是不做任何事情。为 spleen 生成的默认构造函数会自动调用 foo 的基本空构造函数。

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

【讨论】:

  • 而且这个spleen类甚至不需要构造函数——只要声明类继承自foo就会自动调用基类的默认构造函数。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

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

    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)
    }
    

    【讨论】:

    • 嗯,看起来用户编辑了问题。我的回答与问题的原始版本相符。不过,您在更新后是正确的。
    • 是的,我刚刚看了修订版,你第一次看到了。
    【解决方案4】:

    使构造函数受保护:

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

    //东西

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

    以此类推

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

    【讨论】:

    • 基类的构造函数不需要保护。 C# 中的子类可以调用任何公共或受保护的构造函数。
    猜你喜欢
    • 2014-12-27
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多