VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之封装计算方法实现面向对象计算器。


 public abstract class Calculator
    {
        public double Number1 { get; set; }
        public double Number2 { get; set; }
        public abstract double Calculate();
    }
复制代码
复制代码
class Add : Calculator
    {
        public Add(double n1,double n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double Calculate()
        {
            return Number1 + Number2;
        }
    }
复制代码
复制代码
class Sub : Calculator
    {
        public Sub(double n1,double n2 )
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }

        public override double Calculate()
        {

            return Number1 - Number2;
        }
    }
复制代码

 

复制代码
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("第一个数");
            double Number1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("操作符");
            string  Operate= Console.ReadLine();
            Console.WriteLine("第二个数");
            double Number2= Convert.ToDouble(Console.ReadLine());
            Calculator calculator = null;
            switch (Operate)
            {
                case "+":
                    calculator = new Add(Number1, Number2); 
                    break;
                case "-":
                    calculator = new Sub(Number1, Number2);
                    break;
            }
            if (calculator!=null)
            {
                Console.WriteLine("结果是:{0}",calculator.Calculate());
            }
            else
            {
                Console.WriteLine("没有这种操作");
            }
        }
    }
复制代码

优化:

 Calculator calculator = GetComputeObject(Operate, Number1, Number2);

复制代码
 private static Calculator GetComputeObject(string perate, double number1, double number2)
        {
            Calculator result = null;
            switch (perate)
            {
                case "+":
                    result = new Add(number1, number2);
                    break;
                case "-":
                    result = new Sub(number1, number2);
                    break;
            }
            return result;
        }
复制代码
 
 public abstract class Calculator
    {
        public double Number1 { get; set; }
        public double Number2 { get; set; }
        public abstract double Calculate();
    }
复制代码
复制代码
class Add : Calculator
    {
        public Add(double n1,double n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double Calculate()
        {
            return Number1 + Number2;
        }
    }
复制代码
复制代码
class Sub : Calculator
    {
        public Sub(double n1,double n2 )
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }

        public override double Calculate()
        {

            return Number1 - Number2;
        }
    }
复制代码

 

复制代码
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("第一个数");
            double Number1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("操作符");
            string  Operate= Console.ReadLine();
            Console.WriteLine("第二个数");
            double Number2= Convert.ToDouble(Console.ReadLine());
            Calculator calculator = null;
            switch (Operate)
            {
                case "+":
                    calculator = new Add(Number1, Number2); 
                    break;
                case "-":
                    calculator = new Sub(Number1, Number2);
                    break;
            }
            if (calculator!=null)
            {
                Console.WriteLine("结果是:{0}",calculator.Calculate());
            }
            else
            {
                Console.WriteLine("没有这种操作");
            }
        }
    }
复制代码

优化:

 Calculator calculator = GetComputeObject(Operate, Number1, Number2);

复制代码
 private static Calculator GetComputeObject(string perate, double number1, double number2)
        {
            Calculator result = null;
            switch (perate)
            {
                case "+":
                    result = new Add(number1, number2);
                    break;
                case "-":
                    result = new Sub(number1, number2);
                    break;
            }
            return result;
        }
复制代码


相关教程