C#简单工厂模式实例之计算器


提示:以下是本篇文章正文内容,下面案例可供参考

一、操作符的抽象方法(Operation )

定义一个抽象类Operation 作为所有操作符(+、-、*、/)的父类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public abstract class Operation
    {
        private double num1;
        private double num2;
        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public abstract double getResult();  // 需要重写的方法
    }
}

二、创建所有的操作符子类

1.加法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public class Add:Operation
    {

        override
         public double getResult()
        {
            double result = Num1+ Num2;
            return result;
        }

    }
}

2.减法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public class Sub: Operation
    {
        override
        public double getResult()
        {
            double result = Num1 - Num2;
            return result;
        }
    }
}

3.乘法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public class Mul: Operation
    {
        override
        public double getResult()
        {
            double result = Num1 * Num2;
            return result;
        }
    }
}

4.除法类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public class Div:Operation
    {
        override
         public double getResult()
        {
            if (Num2 != 0)
            {
                double result = Num1 / Num2;
                return result;
            }
            else
            {
                return 0;
            }
            
        }
    }
}

三、创建工厂函数

通过工厂函数 new 一个操作符对象,得到结果数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace demo02
{
    public class OperationFactory
    {
		public static Operation getOperation(String oper) //接收传入的操作符
		{
            switch (oper)
            {
				case "+": return new Add();
				case "-": return new Sub();
				case "*": return new Mul();
				case "/": return new Div();
				default:return null;
			}
		}
    }
}

四、页面效果

这里我是用的web页面进行显示输出的
在这里插入图片描述

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="demo02.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 24px;
            width: 932px;
        }
        .auto-style2 {
            height: 24px;
            width: 191px;
        }
        .auto-style3 {
            width: 191px;
        }
        .auto-style4 {
            height: 24px;
            width: 70px;
        }
        .auto-style5 {
            width: 70px;
        }
        .auto-style6 {
            width: 932px;
        }
        .auto-style7 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style7">
                <tr>
                    <td class="auto-style2">
                        <asp:TextBox ID="num1" runat="server" type="number" ></asp:TextBox>
                    </td>
                    <td class="auto-style4">
                        <asp:DropDownList ID="opers" runat="server">
                            <asp:ListItem>+</asp:ListItem>
                            <asp:ListItem>-</asp:ListItem>
                            <asp:ListItem>*</asp:ListItem>
                            <asp:ListItem>/</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td class="auto-style1">
                        <asp:TextBox ID="num2" type="number" runat="server"></asp:TextBox>
&nbsp;=
                        <asp:Label ID="result" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style6">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style5">&nbsp;</td>
                    <td class="auto-style6">
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="计算" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PRINT!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值