zoukankan      html  css  js  c++  java
  • 设计模式

    父类定好模板,不同的子类只需要往里面填个性化的内容。就像一张卷子,不同的考生往里面填各自的答案。

    public class TestPaper
    {
        public void Question1()
        {
            Console.WriteLine("This is Question1:");
            Console.WriteLine(string.Format("Your answer is {0}", Answer1()));
        }
    
        public void Question2()
        {
            Console.WriteLine("This is Question2:");
            Console.WriteLine(string.Format("Your answer is {0}", Answer2()));
        }
    
        protected virtual string Answer1()
        {
            return string.Empty;
        }
    
        protected virtual string Answer2()
        {
            return string.Empty;
        }
    }
    
    public class TestPaperA : TestPaper
    {
        protected override string Answer1()
        {
            return "a";
        }
        protected override string Answer2()
        {
            return "b";
        }
    }
    
    public class TestPaperB : TestPaper
    {
        protected override string Answer1()
        {
            return "aa";
        }
        protected override string Answer2()
        {
            return "bb";
        }
    }
    public class TemplateViewModel
    {
        public TemplateViewModel()
        {
            TestPaper a = new TestPaperA();
            a.Question1();
            a.Question2();
            TestPaper b = new TestPaperB();
            b.Question1();
            b.Question2();
        }
    }
    
  • 相关阅读:
    往鼠标位置写入 诗词
    shell条件判断
    shell 案例一
    Shell中的变量
    echo e 命令详解
    Docker 安装RedisJSON 与使用
    Python 插入数据库的各种方式
    Python压缩文件/文件夹
    shell运算符
    关于CAN总线简单总结
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14141725.html
Copyright © 2011-2022 走看看