【发布时间】:2023-04-05 05:51:01
【问题描述】:
我有一个基类:
public class Base {
public string Foo { get; set; }
public float Bar { get; set; }
public float Foobar { get; set; }
public Base (string foo, float bar, float foobar) {
Foo = foo;
Bar = bar;
Foobar = foobar;
}
}
当我尝试添加扩展它的类时出现错误:
public class Derived : Base {
public Base derived = new Derived ("Foo", 0f, 0f);
}
收到的错误说明如下:Base does not contain a constructor that takes 0 arguments
我在 Derived 类的第 1 行收到此错误。 发生这种情况的任何修复/原因?
【问题讨论】:
-
构造函数不会被继承。在
Derived中声明一个构造函数,它接受相同的参数并使用语法public Derived(...) : base(...)调用基本构造函数。