相关疑难解决方法(0)

如何继承构造函数?

想象一下具有许多构造函数和虚方法的基类

public class Foo
{
   ...
   public Foo() {...}
   public Foo(int i) {...}
   ...
   public virtual void SomethingElse() {...}
   ...
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个覆盖虚方法的后代类:

public class Bar : Foo 
{
   public override void SomethingElse() {...}
}
Run Code Online (Sandbox Code Playgroud)

而另一个后代做了更多的东西:

public class Bah : Bar
{
   public void DoMoreStuff() {...}
}
Run Code Online (Sandbox Code Playgroud)

我是否真的必须将所有构造函数从Foo复制到Bar和Bah?然后,如果我在Foo中更改构造函数签名,我是否必须在Bar和Bah中更新它?

有没有办法继承构造函数?有没有办法鼓励代码重用?

c# inheritance constructor

181
推荐指数
10
解决办法
18万
查看次数

标签 统计

c# ×1

constructor ×1

inheritance ×1