C#继承和对象引用
我是C#的新手,正在试图弄清楚继承是如何工作的。我得到以下错误。为什么父参数必须是静态的 严重性代码说明项目文件行抑制状态 错误CS0120非静态字段需要对象引用, 方法或属性“Rectangle.name”PurrS PATH\Sign.cs 15处于活动状态 家长: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
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)
例如。在这里,我认为参数会自动传递给父类。谢谢。在这里,我认为参数会自动传递给父类。非常感谢。