想象一下具有许多构造函数和虚方法的基类
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中更新它?
有没有办法继承构造函数?有没有办法鼓励代码重用?