【发布时间】:2019-09-23 09:15:52
【问题描述】:
所以我有一个没有构造函数的Vehicle 类。我想创建一个名为VehicleExtended 的新类,它继承来自Vehicle。
错误是:
'Vehicle' 不包含采用 0 个参数的构造函数 (CS1729)
没有构造函数可以继承吗?
注意:我无法编辑基类,因为我只能看到它的元数据。
public class VehicleData : Vehicle
{
[BsonId]public int _id { get; private set;}
public bool IsCompany { get; private set;}
public int OwnerID { get; private set; }
public string modelName { get; private set;}
}
【问题讨论】:
-
每个类都有一个构造函数。
-
不可能没有构造函数。如果您不提供,将生成一个默认的零参数构造函数。您的错误消息表明情况并非如此,而是有一个带有参数的构造函数。
-
能否请您也添加
Vehicles 代码?正如 tkausl 假设基类有一个可能有参数的构造函数。 -
'Vehicle' does not contain a constructor that takes 0 arguments表示它有一个带有 1 个或多个参数的构造函数。 -
您能否提供
Vehicle-class 的元数据。您还说您要创建一个名为VehicleExtended的类,但为一个名为VehicleData的类提供了元数据,您能否澄清所涉及的类的名称?
标签: c# inheritance