超级简单版多态计算器

个人笔记学习黑马四期

一、编写一个操作父类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态计算器
{

    /// <summary>
    /// 计算父类
    /// </summary>
    class Operation
    {
        public int NumA { get; set; }
        public int NumB { get; set; }

        public Operation(int numA, int numB)
        {
            this.NumA = numA;
            this.NumB = numB;
        }
        public virtual int GetRes()
        {
            return 0;
        }
    }
}

二、写出加减乘除的类,继承于操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态计算器
{
    class Add:Operation
    {
        public Add(int numA, int numB)
            : base(numA, numB)
        { 

        }
        public override int GetRes()
        {
            return base.NumB+NumA;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态计算器
{
    class Jian:Operation
    {
        public Jian(int numA, int numB)
            : base(numA, numB)
        { 

        }
        public override int GetRes()
        {
            return base.NumA-NumB;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态计算器
{
    class Ji:Operation
    {
         public Ji(int numA, int numB)
            : base(numA, numB)
        { 

        }
        public override int GetRes()
        {
            return base.NumA*NumB;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态计算器
{
    class Div:Operation
    {
        public Div(int numA, int numB)
            : base(numA, numB)
        { 

        }
        public override int GetRes()
        {
            return base.NumA/NumB;
        }
    }
}

三、写Main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using 多态计算器;

namespace C09多态计算器
{
    class Program
    {
        static void Main(string[] args)
        {
            //多态是基于继承
            //找父类
            //找子类的共同方法
            //找每个子类的同1个方法,具有不同的实现
            Console.WriteLine("请输入第一个数字");
            int numA = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个数字");
            int numB = int.Parse(Console.ReadLine());
            //
            Console.WriteLine("请输入操作符:1.加 2.减 3.乘 4.除");
            string opt = Console.ReadLine();
            Operation operation=null;

             switch (opt)
             {
                 case "1":
                     operation=new Add(numA,numB);
                     break;
                 case "2":
                     operation =new Jian(numA,numB);
                     break;
                 case "3":
                     operation=new Ji(numA,numB);
                     break;
                 case "4":
                     operation =new Div(numA,numB);
                     break;

             }
            Console.WriteLine(operation.GetRes());
            Console.ReadKey();

        }
    }
}

就好了

超级简单版多态计算器

时间: 2024-08-29 06:41:49

超级简单版多态计算器的相关文章

GTK简单版计算器

接下来我们做一个简单版的计算器. 1)获取按钮上的内容. 2)如果获取的内容是" c ",则代表进行退格操作,相当于删去最后一个字符. 3)如果获取的内容不是" c ",则把每一次获取的内容连接起来,如: 第一次按了 " 1 ", 第二次按了 " + ", 第三次按了 " 1 ", 连起来则变为 "1+1". 4)如果获取的内容是" = ",则需要相应的运算.先把连起

JS基础(超级简单)

1     JS基础(超级简单) 1.1 数据类型 1.1.1   基本类型: 1)        Number:特别注意:NaN的检测方法:Nan!=NaN;或者使用isNaN方法 2)        string 3)        boolean 4)        null 5)        undefined 1.1.2   复杂类型 object:date,array,function 1.2 变量 var 变量名=值 变量的作用域:1.全局:以页面为单位.2局部:以函数为单位.

分享一个近期写的简单版的网页采集器

分享一个近期写的简单版的网页采集器 功能特点: 1.可通过配置,保存采集规则. 2.可通过采集规则,进行数据采集. 3.可分页,分关键字,进行采集. 4.可保存数据至数据库,文本中. ........... 功能还比较简单,喜欢深入的可以继续深入下去,暂时还没有登录的功能,因为登录功能涉及到的范围比较广,待日后慢慢研究后再开发. 我先上个图让大家一睹为快吧: 首先看看页面,我们要采集这个网站的文章 接下来,首先是查找分页,获得分页里面的文章链接,接着查找内容页需要采集的字段,生成规则,进行采集.

超级简单实用的.NET性能调试工具-----ANTS Performance Profiler

艺术字做到后期,性能成了一个比较大的瓶颈,优化性能的过程中接触了一些.NET性能调试工具,最喜欢的是ANTS Performance Profiler,操作简单,功能也跟我期望的差不多. ANTS Performance Profiler是出自大名鼎鼎的Red Gate,很多人使用过的.NET Reflector就是出自此公司,该公司还出品其他产品,具体请戳http://www.red-gate.com/products/.ANTS Performance Profiler支持.NET平台所有语

作业1开发一个简单的python计算器

开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致 hint: re.search(r'\([^()]+\)',s).group() '(-

Java对象简单实用(计算器案例)

Java对象简单实用(计算器案例) 对 Java中的对象与属性,方法的使用,简单写了个案例 1 import java.util.Scanner; 2 class Calculste 3 { 4 int a; //定义两个整数 5 int b; 6 String option; //定义接收操作符的字符串 7 public void count(){ 8 9 //对操作符进行判断 10 switch(option){ 11 case "+": 12 System.out.println

Qt版科学计算器

Qt版科学计算器 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 之前做过<VC版科学计算器>,这也是我学VC++时的第一个大作业,通过科学计算器的开发使用我学到了很多东西,也让我逐渐喜欢上了编程.最近在学习Qt,所以将当时在VC下写过的一些东西在Qt下重写了一遍,其实主要还是与显示等有关的东西需要重写,要使用Qt的显示方式,而其他的核心的算法等都还是使用VC下C++的源码. 下面是Qt版的运行截图: 标准版: 科学版: 头文件中变量和槽的声明: c

Java实现简单版SVM

最近的图像分类工作要用到latent svm,为了更加深入了解svm,自己动手实现一个简单版的. 之所以说是简单版,因为没有用到拉格朗日,对偶,核函数等等.而是用最简单的梯度下降法求解.其中的数学原理我参考了http://blog.csdn.net/lifeitengup/article/details/10951655,文中是用matlab实现的svm. 源代码和数据集下载:https://github.com/linger2012/simpleSvm 其中数据集来自于libsvm,我找了其中

MFC抓取网页代码简单版。

最近又在网上找了一些有关MFC抓取网页代码的文章看,发现有个比较简单的代码,和大家分享下. CInternetSession session(NULL, 0); CHttpFile* htmlFile = NULL; CString strLine, strHtml; CString url = _T("http://www.tqyb.com.cn/data/gzWeather/gz_weatherForecastInDays.js?"); TCHAR sRecv[1024]; UIN