gpt4 book ai didi

c# - 这种继承设计模式的名称是什么?

转载 作者:太空狗 更新时间:2023-10-30 01:16:17 25 4
gpt4 key购买 nike

您有一个调用 protected 虚方法的公共(public)密封方法,而不是公共(public)虚方法。像这样:

public class Test {

public void DoStuff(){
// Do stuff here...
ProtectedDoStuff();
// Do more stuff...
}

protected virtual void ProtectedDoStuff(){
// Do stuff...
}
}

代替:

public class Test {

public virtual void DoStuff(){
// Do stuff here...
// Do a lot of stuff...
// Do more stuff...
}
}

public class Test2 : Test {

public override void DoStuff(){
// Do same stuff as base
// Do different stuff
// Do more stuff just like base
}
}

这避免了在所有时间都需要的情况下必须重新实现公共(public)方法中的所有功能。我知道这已经在 stackoverflow 上被问到了,但我找不到问题。

最佳答案

这是模板方法模式。来自 Wikipedia :

The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure.

关于c# - 这种继承设计模式的名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719790/

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