有没有办法只将代码添加到子类中的方法而不像 c# 中的“覆盖”+“虚拟”那样完全覆盖它。我发现自己在重写方法中编写了一些重复的代码。不知道该怎么办
问问题
162 次
1 回答
3
您可以使用具有不同机制的覆盖和虚拟。例如,
class MyBase
{
private int MyVar;
public virtual void DoStuff(int i , int j)
{
MyVar = i + j; //This is your common code which is added in base class
}
}
class OverridClass : MyBase
{
private int MyNewCount;
public override void DoStuff(int i, int j)
{
MyNewCount = i + j;
base.DoStuff(i, j); //This is how you reuse your common code and write the code which is more specific to this method
}
}`
于 2013-09-28T14:32:29.003 回答