【发布时间】:2011-12-25 16:07:16
【问题描述】:
我有一个关于在基类中保存公共代码并让派生类调用它的问题,即使派生类的触发器方法已从基类分派。所以,base->derived->base type call stack.
下面的看起来还可以,还是有味道?我已经对流程步骤进行了编号...
public abstract class LayerSuperType
{
public void DoSomething() // 1) Initial call from client
{
ImplementThis(); // 2) Polymorphic dispatch
}
protected abstract void ImplementThis();
protected void SomeCommonMethodToSaveOnDuplication(string key) // 4)
{
Configuration config = GetConfiguration(key);
}
}
public class DerivedOne : LayerSuperType
{
protected virtual void ImplementThis() // 2)
{
SomeCommonMethodToSaveOnDuplication("whatever"); // 3) Call method in base
}
}
public class DerivedTwo : LayerSuperType
{
protected virtual void ImplementThis() // 2)
{
SomeCommonMethodToSaveOnDuplication("something else"); // 3) Call method in base
}
}
【问题讨论】:
标签: c# oop inheritance polymorphism