• 简单工厂设计模式计算器


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂设计模式计算器
    {
        class Program
        {
            static void Main(string[] args)
            {
                //虚方法、抽象类、接口
                //面向对象计算器 案例
                while (true)
                {
                    Console.WriteLine("请输入第一个数字");
                    double n1 = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("请输入第二个数字");
                    double n2 = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("请输入运算符");
                    string opera = Console.ReadLine();
                    CalFather cal = GetCal(opera, n1, n2);//通过opera操作符参数判断生成不同的子类赋值给父类 并返回
                    double res = cal.GetResult();//调用的父类抽象方法其实是被子类的override重写后的方法
                    Console.WriteLine(res);
                    Console.ReadKey();
                }
               
            }
    
            public static CalFather GetCal(string opear, double n1, double n2)
            {
                CalFather cal = null;
                switch (opear)
                {
                    case "+": cal = new Add(n1, n2);
                        break;
                    case "-": cal = new Sub(n1, n2);
                        break;
                    case "*": cal = new Cheng(n1, n2);
                        break;
                    case "/": cal = new Chu(n1, n2);
                        break;
                }
                return cal;
            }
        }
    
    
        public abstract class CalFather
        {
            public double NumberOne
            {
                get;
                set;
            }
    
            public double NumberTwo
            {
                get;
                set;
            }
    
            public CalFather(double n1, double n2)
            {
                this.NumberOne = n1;
                this.NumberTwo = n2;
            }
    
    
            public abstract double GetResult();
        }
    
    
        public class Add : CalFather
        {
            public Add(double n1, double n2)
                : base(n1, n2)
            {
    
            }
    
            public override double GetResult()
            {
                return this.NumberOne + this.NumberTwo;
            }
        }
    
        public class Sub : CalFather
        {
            public Sub(double n1, double n2)
                : base(n1, n2)
            {
    
            }
    
            public override double GetResult()
            {
                return this.NumberOne - this.NumberTwo;
            }
        }
    
        public class Cheng : CalFather
        {
            public Cheng(double n1, double n2)
                : base(n1, n2)
            {
    
            }
    
            public override double GetResult()
            {
                return this.NumberOne * this.NumberTwo;
            }
        }
    
        public class Chu : CalFather
        {
            public Chu(double n1, double n2)
                : base(n1, n2)
            {
    
            }
    
            public override double GetResult()
            {
                return this.NumberOne / this.NumberTwo;
            }
        }
    }
  • 相关阅读:
    mvn command is not recognized as an internal or external command
    mac搭建nginx和wordpress开发环境
    搭建nginx+php环境时遇到”file not found"的问题
    MAC下bash配置文件的加载顺序
    lsof的注意事项
    理解字符集、字符编码、ASCII、Unicode、UTF-8
    初学markdown有感
    AngularJS开发指南-Animations
    #正则表达式# 学习笔记
    vi学习笔记/基本操作方法
  • 原文地址:https://www.cnblogs.com/blacop/p/5986278.html
Copyright © 2020-2023  润新知