一个主要的窗体措施,两个输入框,一个label
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ProOperation;
using ProFactory;
namespace ProOperation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//首先读取配置文件
string[] lines = File.ReadAllLines("Config.txt", Encoding.Default);
int x = 139;
int y = 154;
foreach (string item in lines)
{
//有几条数据,就创建几个按钮
Button btn = new Button();
btn.Location = new Point(x,y);
btn.Size = new Size(75, 23);
x += 80;
btn.Text = item;
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);//添加到窗体上
}
}
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
int n1 = Convert.ToInt32(textBox1.Text.Trim());
int n2 = Convert.ToInt32(textBox2.Text.Trim());
//获得简单工厂供给的父类东西
Operation oper = Factory.GetOper(btn.Text, n1, n2);
if (oper != null)
{
label1.Text = oper.GetResult().ToString();
}
else
{
MessageBox.Show("并没有此运算符");
}
}
}
}
四个类库孕育产生dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProOperation;
namespace ProAdd
{
public class Add : Operation
{
//默认是挪用的是无参结构。如果有参的话,就要这样挪用,将参数,传给父类
public Add(int n1, int n2) : base(n1, n2){}
public override string Type
{
get { return "+"; }
}
public override int GetResult()
{
return this.NumberOne + NumberTwo;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProOperation;
using System.Reflection;
using System.IO;