Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#继承和对象引用_C#_Inheritance - Fatal编程技术网

C#继承和对象引用

C#继承和对象引用,c#,inheritance,C#,Inheritance,我是C#的新手,正在试图弄清楚继承是如何工作的。我得到以下错误。为什么父参数必须是静态的 严重性代码说明项目文件行抑制状态 错误CS0120非静态字段需要对象引用, 方法或属性“Rectangle.name”PurrS PATH\Sign.cs 15处于活动状态 家长: namespace PurrS.Maps { public class Rectangle { protected string name; protected int id; protected flo

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

严重性代码说明项目文件行抑制状态 错误CS0120非静态字段需要对象引用, 方法或属性“Rectangle.name”PurrS PATH\Sign.cs 15处于活动状态

家长:

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;

    }
}
}

您需要对此进行扩展:

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

您需要对此进行扩展:

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

您必须使用更多参数从构造函数中传递参数

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;

        }

您必须使用更多参数从构造函数中传递参数

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;

        }

继承意味着子类(符号类)将拥有父类中的所有字段和方法。所以你可以说

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

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

继承意味着子类(符号类)将拥有父类中的所有字段和方法。所以你可以说

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

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

您看到的问题源于这样一个事实:
Rectangle
有一个构造函数-创建
Rectangle
实例的唯一方法是将8个参数传递给它

当您创建从
矩形
继承的
符号
时-因为它是一个
矩形
-它需要能够调用它的
矩形
构造函数才能成功地构造自己

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

您可以在
符号
中要求参数,或在
符号
构造函数中硬编码参数:

 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)

例如。

您看到的问题源于这样一个事实:
Rectangle
有一个构造函数-创建
Rectangle
实例的唯一方法是将8个参数传递给它

当您创建从
矩形
继承的
符号
时-因为它是一个
矩形
-它需要能够调用它的
矩形
构造函数才能成功地构造自己

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

您可以在
符号
中要求参数,或在
符号
构造函数中硬编码参数:

 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)

例如。

在这里,我认为参数会自动传递给父类。谢谢。在这里,我认为参数会自动传递给父类。非常感谢。