标签:
考题试卷示例:
1.试卷父类:
public class TestPaper { public void TestQuestion1() { Console.WriteLine("杨过是哪部小说中的人物?a.飞狐外传 b.天龙八部 c.射雕英雄传 d.笑傲江湖"); Console.WriteLine("答案:"+Answer1()); } public void TestQuestion2() { Console.WriteLine("令狐冲是哪部小说中的人物?a.飞狐外传 b.天龙八部 c.射雕英雄传 d.笑傲江湖"); Console.WriteLine("答案:" + Answer2()); } public virtual string Answer1() { return ""; } public virtual string Answer2() { return ""; } }
2.学生甲抄的试卷类:
public class TestPaperA:TestPaper { public override string Answer1() { return "c"; } public override string Answer2() { return "a"; } }
3.学生乙抄的试卷类:
class TestPaperB:TestPaper { public override string Answer1() { return "b"; } public override string Answer2() { return "d"; } }
4.客户端代码:
class Program { static void Main(string[] args) { Console.WriteLine("学生甲抄的试卷:"); TestPaper studentA = new TestPaperA(); studentA.TestQuestion1(); studentA.TestQuestion2(); Console.WriteLine("学生乙抄的试卷:"); TestPaper studentB = new TestPaperB(); studentB.TestQuestion1(); studentB.TestQuestion2(); } }
当我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑使用模板方法模式来处理。
标签:
原文地址:http://www.cnblogs.com/walden1024/p/4494938.html