简单工厂设计模式计算器
来源:互联网 发布:如何描述一个java项目 编辑:程序博客网 时间:2024/05/21 11:13
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; } }}
0 0
- 简单工厂设计模式计算器
- 利用简单工厂模式设计一个计算器
- C#简单工厂设计模式实现计算器
- java简单工厂设计模式实现计算器
- 计算器 --- 简单工厂模式
- 简单工厂模式-计算器
- 简单工厂模式计算器
- 简单工厂模式-计算器
- 简单工厂模式---计算器
- 简单工厂模式-计算器
- 设计模式一:简单工厂模式(计算器)
- 黑马程序员:初识设计模式,简单工厂模式的计算器
- 大话设计模式之简单工厂模式---------计算器
- 利用简单工厂模式设计一个计算器的程序
- 简单工厂模式----控制台计算器的设计(C++实现)
- 大话设计模式学习笔记--简易计算器(简单工厂)
- 简单工厂设计模式实现计算器的案例
- 简单工厂模式-计算器程序设计
- NOJ 1543 冒泡排序
- 13.高级IO和多线程和线程同步
- 第一个数字
- Odoo 10.0 安装自定义模块
- Presto源码分析(hive-rcfile)
- 简单工厂设计模式计算器
- HTTP的request部分的学习
- 二维数组与指针
- SQLite简单运用
- 计算机的内存
- Android之获取图片缩略图
- keepalived 高可用集群
- 【JZOJ1611】Dining
- 1003. Maze