面向对象编写一个计算器

首先分析计算器有加减乘除,取模,求余等计算方式,所以我们在创建了这个类的同时就应该想到后期肯定需要进行这方面的扩展,所以在设计的时候就应该尽量不修改原有代码的前提下进行功能的扩展,也就是降低对象之间的耦合度。

废话不多说,代码如下:

namespace Program
{
    //显示抽象类
    public abstract class Print
    {
        public decimal Result { get; set; }
        public Print(decimal result)
        {
            this.Result = result;
        }
        public abstract void print();
    }
    public class shuchu1 : Print
    {
        public shuchu1(decimal result) : base(result) { }
        public override void print()
        {
            Console.WriteLine("输出方式1输出" + Result);
        }
    }
    public class shuchu2 : Print
    {
        public shuchu2(decimal result) : base(result) { }
        public override void print()
        {
            Console.WriteLine("输出方式2输出" + Result);
        }
    }
    //计算抽象类
    public abstract class Opreation
    {
        public decimal Num1 { get; set; }
        public decimal Num2 { get; set; }
        public virtual decimal OpreationNum()
        {
            decimal result = 0;
            return result;
        }
    }
    #region CalculateMode
    //加法运算
    public class addition : Opreation
    {
        public override decimal OpreationNum()
        {
            return this.Num1 + this.Num2;
        }
    }

    //减法
    public class subtraction : Opreation
    {
        public override decimal OpreationNum()
        {
            return this.Num1 - this.Num2;
        }
    }
    public class multiplication : Opreation
    {
        public override decimal OpreationNum()
        {
            return this.Num1 * this.Num2;
        }
    }
    public class division : Opreation
    {
        public override decimal OpreationNum()
        {
            return this.Num1 / this.Num2;
        }
    }
    #endregion


    public abstract class CalculateFactory
    {
        public abstract Opreation Calculate();
    }
    #region CalculateFactory
    public class AddtionFactory : CalculateFactory
    {
        public override Opreation Calculate()
        {
            return new addition();
        }
    }
    public class SubtractionFactory : CalculateFactory
    {
        public override Opreation Calculate()
        {
            return new subtraction();
        }
    }
    public class MultiplicationFactory : CalculateFactory
    {
        public override Opreation Calculate()
        {
            return new multiplication();
        }
    }
    public class DivisionFactory : CalculateFactory
    {
        public override Opreation Calculate()
        {
            return new division();
        }
    }
    #endregion
    //取模计算
    public class Molding : Opreation
    {
        public override decimal OpreationNum()
        {
            return this.Num1 % this.Num2;
        }
    }
    //取模工厂
    public class MoldingFactory : CalculateFactory
    {
        public override Opreation Calculate()
        {
            return new Molding();
        }
    }
    //取模值显示方式
    public class shuchu3 : Print
    {
        public shuchu3(decimal result) : base(result) { }
        public override void print()
        {
            Console.WriteLine("显示方式3输出" + Result);
        }
    }
    public class Calculator
    {
        static void Main(string[] args)
        {
            CalculateFactory opreationAddtion = new AddtionFactory();
            Opreation addition = opreationAddtion.Calculate();
            addition.Num1 = 10;
            addition.Num2 = 20;
            Print print1 = new shuchu1(addition.OpreationNum());
            print1.print();

            CalculateFactory opreationSubtraction = new SubtractionFactory();
            Opreation Subtraction = opreationSubtraction.Calculate();
            Subtraction.Num1 = 30;
            Subtraction.Num2 = 10;
            Subtraction.OpreationNum();
            Print print2 = new shuchu2(Subtraction.OpreationNum());
            print2.print();

            CalculateFactory opreationMolding = new MoldingFactory();
            Opreation molding = opreationMolding.Calculate();
            molding.Num1 = 10;
            molding.Num2 = 8;
            Print print3 = new shuchu3(molding.OpreationNum());
            print3.print();
        }
    }
}

 

展开阅读全文

没有更多推荐了,返回首页