zoukankan      html  css  js  c++  java
  • 十一.模板模式

    模板方法:

           定义一个操作的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

    Demo1

           classquestion

        {

           public void ShowQ()

           {

               Console.WriteLine("问题:");

               Console.WriteLine("答案:"+ShowA());

           }

           public virtual string  ShowA()

           {

               return "";

           }

        }

       class question1:question

        {

           public override string ShowA()

           {

               return "A";

           }

        }

    Demo2:

           classfather

        {

           public void Show1()

           {

                Console.WriteLine("这是方法1");

           }

           public virtual void Show2()

           {

               Console.WriteLine("这是可变方法2");

           }

           public void ShowAll()

           {

               Show1();

               Show2();

           }

        }

       class child : father

        {

           public override void Show2()

           {

               base.Show2();

               Console.WriteLine("变变看");

           }

        }

  • 相关阅读:
    Valid Parentheses
    Remove Nth Node From End of List
    守护线程、守护进程
    4Sum
    Letter Combinations of a Phone Number
    3sum closest
    Excel Sheet Column Number
    Majority Element
    Balanced Binary Tree
    Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/yaoge/p/1815232.html
Copyright © 2011-2022 走看看