【问题标题】:C# inheritance and Object referenceC#继承和对象引用
【发布时间】:2016-02-29 21:29:59
【问题描述】:

我是 C# 新手,并试图弄清楚继承是如何工作的。我收到以下错误。为什么父参数必须是静态的?

严重性代码描述项目文件行抑制状态 错误 CS0120 非静态字段需要对象引用, 方法或属性 'Rectangle.name' PurrS PATH\Sign.cs 15 Active

家长:

namespace PurrS.Maps
{
public class Rectangle
{
    protected string name;
    protected int id;
    protected float a;
    protected float b;
    protected float c;
    protected float d;
    protected int posX;
    protected int posY;
    //A----B
    //|    | 
    //C----D

    public Rectangle(string name, int id, float a, float b, float c, float d, int posX, int posY)
    {
        this.name = name;
        this.id = id;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

}
}

孩子:

namespace PurrS.Maps
{

public class Sign : Rectangle
{
    string message;

    public Sign(string message) 
        : base(name, id, a, b, c, d, posX, posY) { //This is where it fails.
        this.message = message;

    }
}
}

【问题讨论】:

    标签: c# inheritance


    【解决方案1】:

    您看到的问题源于 Rectangle 有一个构造函数这一事实 - 创建 Rectangle 实例的唯一方法是向其传递 8 个参数。

    当您创建一个继承自 RectangleSign - 因为它 Rectangle - 它需要能够调用其 Rectangle 构造函数才能成功构造自己。

    因此,在调用Rectangle(您只有一个)上的构造函数时,它需要所有可用参数。

    您可以在Sign 中请求参数,或者在Sign 构造函数中对其进行硬编码:

     public Sign(string message, string name, int id, float a, float b, float c, float d, int posX, int posY) 
         :base(name,id,a,b,c,d,posX,poxY)
    
     public Sign(string message) : base("a name", 1, 1, 2, 3, 4, 10, 10)
    

    例如。

    【讨论】:

      【解决方案2】:

      您必须从带有更多参数的构造函数传递参数

      public Sign(string name, int id, float a, float b, float c, float d, int posX, int posY, string message)
                      : base(name, id, a, b, c, d, posX, posY)
                  { //This is where it fails.
                      this.message = message;
      
                  }
      

      或提供一些默认的固定值:

              public Sign(string message)
                  : base("foo", 1, 0, 0, 0, 0, 1, 1)
              { //This is where it fails.
                  this.message = message;
      
              }
      

      【讨论】:

      • 在这里我认为参数会自动传递给父类。谢谢。
      【解决方案3】:

      您需要对此进行扩展:

      public Sign(string message) 
          : base(name, id, a, b, c, d, posX, posY) { //This is where it fails.
          this.message = message;
      }
      

      将参数传递给基类如下:

      public Sign(string message, string name, int id, etc...) 
          : base(name, id, a, b, c, d, posX, posY) { 
          this.message = message;
      }
      

      【讨论】:

        【解决方案4】:

        继承意味着您的子类(Sign 类)将拥有父类中的所有字段和方法。因此,你可以说

        public Sign(string name, int id, float a, float b, float c, float d, int posX, int posY)
            {
                this.name = name;
                this.id = id;
                this.a = a;
                this.b = b;
                this.c = c;
                this.d = d;
            }
        

        并且不必声明您正在使用的任何字段,因为它们是从父类继承的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-04
          • 1970-01-01
          • 1970-01-01
          • 2012-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多