文章出處

// 客戶端調用
    class Client
    {
        static void Main(string[] args)
        {
            // 創建一個菠菜實例并調用模板方法
            Spinach spinach = new Spinach();
            spinach.CookVegetabel();
            Console.Read();
        }
    }

    public abstract class Vegetabel
    {
        // 模板方法,不要把模版方法定義為Virtual或abstract方法,避免被子類重寫,防止更改流程的執行順序
        public  void CookVegetabel()
        {
            Console.WriteLine("抄蔬菜的一般做法");
            this.pourOil();
            this.HeatOil();
            this.pourVegetable();
            this.stir_fry();
        }

        // 第一步倒油
        public  void pourOil()
        {
            Console.WriteLine("倒油");
        }

        // 把油燒熱
        public  void HeatOil()
        {
            Console.WriteLine("把油燒熱");
        }

        // 油熱了之后倒蔬菜下去,具體哪種蔬菜由子類決定
        public abstract void pourVegetable();

        // 開發翻炒蔬菜
        public  void stir_fry()
        {
            Console.WriteLine("翻炒");
        }
    }

    // 菠菜
    public class Spinach : Vegetabel
    {
       
        public override void pourVegetable()
        {
            Console.WriteLine("倒菠菜進鍋中");
        }
    }

    // 大白菜
    public class ChineseCabbage : Vegetabel
    {      
        public override void pourVegetable()
        {
            Console.WriteLine("倒大白菜進鍋中");
        }
    }

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()