【发布时间】:2021-10-28 08:13:30
【问题描述】:
我有一个基类,它在构造函数中接受一个参数 然后我创建了一个子类,并且还使用了带有 on 参数的构造函数。
想法是DeviceA的构造函数覆盖了GenericDevice的构造函数
但是系统报错
Error CS7036 There is no argument given that corresponds to the required formal parameter 'address' of 'GenericDevice.GenericDevice(string)'
不知道为什么会出现这个错误,因为DeviceA不需要GenericDevice的构造函数,它有自己的
class GenericDevice {
GenericDevice(string address){
}
void Connect() {
}
}
class DeviceA : GenericDevice
{
DeviceA(string address) {
}
void Foo() {
}
}
【问题讨论】:
-
DeviceA(string address) base(address) {... -
将你的
DeviceA构造函数声明更改为:DeviceA(string address) : base(address) {- 必须告诉编译器如何调用基类中的构造函数。 -
是的,如果要粘贴代码,请粘贴实际代码
-
是的,这似乎有效,谢谢
标签: c#