设计模式 模板模式

先写一个父类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prototype
{
    class TestPaper
    {
        public void TestQuestion1()
        {
            Console.WriteLine("杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是【】");
            Console.WriteLine("a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 ");
            Console.WriteLine("答案:" + Answer1());
        }

        protected virtual string Answer1()
        {
            return "";
        }

        public void TestQuestion2()
        {
            Console.WriteLine("杨过、程英、陆无双铲除了情花,造成【】");
            Console.WriteLine("a.这种植物不在害人 b.使一种珍惜物种灭绝 c.破话了那个生物圈的生态平衡 d.造成该地区沙漠化 ");
            Console.WriteLine("答案:" + Answer2());
        }

        protected virtual string Answer2()
        {
            return "";
        }

        public void TestQuestion3()
        {
            Console.WriteLine("蓝凤凰致使华山师徒、套鼓六仙呕吐不止,如果你是大夫,会给他们呢开什么药【】");
            Console.WriteLine("a.阿司匹林 b.牛黄解毒丸 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对 ");
            Console.WriteLine("答案:" + Answer3());
        }

        protected virtual string Answer3()
        {
            return "";
        }
    }
}


在写两个类重写夫级的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prototype
{
    class TestPaperA:TestPaper
    {
        protected override string Answer1()
        {
            return "b";
        }

        protected override string Answer2()
        {
            return "c";
        }

        protected override string Answer3()
        {
            return "a";
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prototype
{
    class TestPaperB:TestPaper
    {
        protected override string Answer1()
        {
            return "c";
        }

        protected override string Answer2()
        {
            return "a";
        }

        protected override string Answer3()
        {
            return "b";
        }
    }
}

最后写测试类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prototype
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("学生甲:");
            TestPaper studentA = new TestPaperA();
            studentA.TestQuestion1();
            studentA.TestQuestion2();
            studentA.TestQuestion3();

            Console.WriteLine("学生乙:");
            TestPaper studentB = new TestPaperB();
            studentB.TestQuestion1();
            studentB.TestQuestion2();
            studentB.TestQuestion3();

            Console.Read();
        }
    }
}

posted @ 2016-11-02 22:27  yufenghou  阅读(121)  评论(0编辑  收藏  举报