C#の単純なファクトリパターンの例の計算機


ヒント:以下はこの記事の内容です。以下のケースは参照用です。

1.演算子の抽象メソッド(操作)

抽象クラス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();  // 需要重写的方法
    }
}

2.すべての演算子サブクラスを作成します

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;
            }
            
        }
    }
}

3、ファクトリ関数を作成します

ファクトリ関数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>

おすすめ

転載: blog.csdn.net/qq_48592827/article/details/115266173
おすすめ