【问题标题】:Can I inherit from a class if base class doesn't have constructor如果基类没有构造函数,我可以从类继承吗
【发布时间】: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


【解决方案1】:

从错误信息中我们可以看到,基类Vehicle没有构造函数没有参数

  public class Vehicle { 
    ...
    // every Vehicle constructors want some parameters, e.g. id and ownerId
    public Vehicle(int id, int ownerId) {...}
    ...
  }

那是编译器不知道如何创建VehicleData的实例:

  // Since VehicleData is inherited from Vehicle, 
  // Vehicle cosntructor should be executed.
  // What arguments should be passed to it?   
  var test = new VehicleData();

你必须手动实现VehicleData构造函数,你应该在其中指定参数:

  public class VehicleData : Vehicle 
  {
    // When creating an instace, use -1 and -1 when calling base constructor
    public VehicleData() : base(-1, -1) {}

    ...
  }

现在上面的代码是合法的

  // We don't want parameters when creating VehicleData instance
  // When creating base class (Vehicle) -1 and -1 are used  
  var test = new VehicleData();

编辑:如果基类 (Vehicle) 没有任何publicprotected构造函数(但@ 987654331@ 个)你不能从VehicleData 创建基础Vehicle 实例,因此不能从Vehicle 继承。 private 构造函数只是一个旧的 C++ 技巧;在 C# 的情况下,为了防止继承,我们应该使用sealed

【讨论】:

  • 您也可以对单例使用私有构造函数。所以它们并非完全无用 - 只是一个旁白。
【解决方案2】:

有可能,基类有一个无法访问的 private 构造函数。在这种情况下,您不能继承该类。

【讨论】:

  • 那会抛出一个Vehicle not accessable due to protecteton-level-error。
  • @HimBromBeere 取决于您如何看待它。如果您将参数添加到私有构造函数,您将收到此消息,在这种情况下,这个答案是正确的,因为您不能继承该类。
  • @Knoop 但是关于附加参数,而不是构造函数是私有的。
  • @HimBromBeere 不会神奇地使继承它成为可能。这个答案表明它有可能有一个私有构造函数(没有说明该构造函数是否有参数)并说明在这种情况下你不能继承这个类。
  • @Knoop 但是 OP 的问题不是为什么我们不能继承具有私有构造函数的类,而是为什么会出现 OP 错误。正如我所看到的,错误是 not 由于私有构造函数,所以争论当一个类有私有构造函数时我们能做什么是没有意义的。
【解决方案3】:

根据错误消息,我假设VehicleData 类有一个私有零参数构造函数。 在这种情况下,您不能从VehicleData 继承。 例如

public class A
{
    private A() {}
}
public class B : A
{
}

这不会编译。 但是,如果有一个 public 或 protected 使用非 zeor 参数列表,您可以从该类继承:

public class A
{
    private A() {}
    public A(int i) {}
}
public class B : A
{
    public B() : base(0) {}
}

此外,如果事实证明 Vehicle 只有 private 构造函数,并且您只想添加特定行为而不是任何无法从 Vehicle 中已定义的属性计算的属性,而是继承你可以使用扩展方法。

public static VehicleExtesnion
{
    public SomeType DoStuff(this Vehicle vehicle) 
    {
        // do stuff with vehicle
    }
}

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 2017-03-11
    相关资源
    最近更新 更多