Skip to main content
 首页 » 编程设计

c#之派生类中的构造函数错误

2024年02月05日16over140

我有一个父类

abstract class ClassPlugin 
{ 
    public ClassPlugin(eGuiType _guyType) 
    { 
        GuiType = _guyType; 
    } 
    public eGuiType GuiType; 
 
    public void Action() { } 
} 

我希望所有派生类都实现 Action 成员并定义 guyType 字段。 为此,我将其放入构造函数中,这是我想到的唯一方法。 因此抽象类可以是:

class ClassStartWF: ClassPlugin 
{ 
    public ClassStartWF(eGuiType _guyType) 
    { 
        GuiType = _guyType; 
    } 
 
    public void Action() 
    { 
        MessageBox.Show(GetType().Name); 
    } 
} 

问题是我得到了错误:

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter '_guyType' of 'MainWindow.ClassPlugin.ClassPlugin(MainWindow.eGuiType)'

请您参考如下方法:

您应该在派生类构造函数中调用基类构造函数。由于您已经创建了构造函数,因此编译器不会为您的类生成默认构造函数。

public ClassStartWF(eGuiType _guyType) 
   : base(_guyType) 
{ 
    // GuiType = _guyType; //this is not needed since the value is assigned in the base class constructor 
}