即我有一个类 Box,其属性为长度、高度、宽度和构造函数“Box(int witdth, int height, int length)”。获得了一个具有属性名称的继承类 ProductBox。 我还有一个 .csv 文件,我使用 FileHelpers 库将其解析为 ProductBox,因此我的构造函数为空。 现在我不能从 Box 继承 ProductBox,因为 Box 有非空的构造函数,而在 FileHelpers 中构造函数是空的。有没有办法从 Box 继承 ProductBox?
class Box
{
int Width;
int Height;
int Length;
public Box(int width, int height, int length)
{
this.Width = width;
this.Height = height;
this.Length = length;
}
}
class ProductBox : Box
{
string Name;
int Width;
int Height;
int Length;
public static ProductBox[] GetInfo(string filePath)
{
var engine = new FileHelperEngine<ProductBox>();
var result = engine.ReadFile(filePath);
return result;
}
}
请您参考如下方法:
没有。
如果你的基类提供了一个有参数的构造函数,你就不能在派生类中提供一个无参数的构造函数,除非你重新定义基类也有一个无参数的构造函数。值(value)从何而来?
如果您的基础实际上是一个 struct,那将是可能的,因为 struct 定义了默认的无参数构造函数。
您可以按照 hl3mukkel 的描述链接您的构造函数,其中您的派生类传递构造的值。你也可以这样做:
class ProductBox : Box
{
string Name;
public ProductBox()
:base(0,0,0)
{
}
}
如果您想自己提供默认值。
