简体   繁体   English

在 C# 中的抽象 class 中的具体方法中调用抽象方法

[英]calling an abstract method in a concrete method inside an abstract class in C#

I have below scenario where an abstract class has an abstract method of a class type and the abstract method has been called inside a concrete method of the same abstract class.我有以下场景,其中抽象 class 具有 class 类型的抽象方法,并且在同一抽象 class 的具体方法中调用了抽象方法。

Can someone explain the below concept of OOPS?有人可以解释下面的OOPS概念吗? I am sorry if I am asking silly Q.如果我问愚蠢的Q,我很抱歉。

using FactoryPattern.PizzaProduct;

namespace FactoryPattern.PizzaStore
{
    public abstract class PizzaStore
    {
        public Pizza OrderPizza(PizzaType type)
        {
            var pizza = CreatePizza(type);
            pizza.Prepare();
            pizza.Bake(); 
            pizza.Cut();
            pizza.Box();
            return pizza;
        }
        protected abstract Pizza CreatePizza(PizzaType type);
    }
}

This is called Template Method pattern .这称为模板方法模式

This pattern allows children class to implement only the relevant bit of logic without the knowledge that is encapsulated in the parent class.此模式允许子 class 仅实现相关的逻辑位,而无需封装在父 class 中的知识。

In your words, it allows every specific pizza store to define some characteristics of a pizza without knowing how the pizza must be prepared, baked, cutted, boxed.用您的话来说,它允许每个特定的比萨店定义比萨的一些特征,而无需知道比萨必须如何准备、烘焙、切割、装盒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM