gpt4 book ai didi

c# - OOD、继承和层父类(super class)型

转载 作者:行者123 更新时间:2023-11-30 15:41:45 24 4
gpt4 key购买 nike

我有一个关于在基类中保存公共(public)代码并让派生类调用它的问题,即使派生类的触发器方法已从基类中分派(dispatch)。所以,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
}
}

最佳答案

看起来绝对不错。为什么要在接口(interface)上使用抽象类的完美示例。它有点像一种策略模式,我经常成功地使用它。

确保类(class)所做的仍然是处理一个“问题”,只做一项任务。如果您的基类进行存储库访问,但对象代表文档,请不要将功能放在基类中,使用单独的存储库模式/对象。

关于c# - OOD、继承和层父类(super class)型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com