-----------------------2010/10/18/22:32-----------------------------------------------------------
首先我必须感谢Green,没有他的帮助不可能这么快找到错误……
如果你看过第一篇笔记,你会发现一个正常人不会犯的错误
在Operation类里,NumberB属性的get居然被我写成了return numberA……
好吧,这就是我一直纠结的问题,郁闷啊
此外,计算器的另外一些问题在Green的帮助下也解决了,具体还是看代码吧……
首先这是Operation类,嗯,改正的
---------Operation.cs------------------
- public class operation
- {
- private double numberA = 0;
- private double numberB = 0;
- public double NumberA
- {
- get { return numberA; }
- set { numberA = value; }
- }
- public double NumberB
- {
- get { return numberB; }
- set { numberB = value; }
- }
- public virtual double GetResult()
- {
- return 0;
- }
- }
具体的运算类没有改变
--------allOperation.cs---------------------
- class operationAdd : operation
- {
- public override double GetResult()
- {
- double result = 0;
- result = NumberA + NumberB;
- return result;
- }
- }
- class operationSub : operation
- {
- public override double GetResult()
- {
- double result = 0;
- result = NumberA - NumberB;
- return result;
- }
- }
------------IFactory.cs-------------------------
- interface Ifactory
- {
- operation createOperator();
- }
都没变
----------allFactories.cs----------------------------
- class addFactory : Ifactory
- {
- public operation createOperator()
- {
- operation oper = new operationAdd();
- return oper;
- }
- }
- class subFactory : Ifactory
- {
- public operation createOperator()
- {
- operation oper = new operationSub();
- return oper;
- }
- }
下面是客户端,改得最严重的地方
--------------client---------------------------------
- public partial class MainPage : PhoneApplicationPage
- {
- operation oper;
- Ifactory operFactory;
- bool isNumASet = true; //是否设置NumA
- double numA = 0;
- double numB = 0;
- public MainPage()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "1";
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "2";
- }
- private void button3_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "3";
- }
- private void button4_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text += "4";
- }
- private void setNum()
- {
- if (textBox1.Text.Length == 0)
- {
- numB = numA;
- return;
- }
- if (isNumASet == true)
- {
- numA = Convert.ToDouble(textBox1.Text);
- isNumASet = false;
- textBox1.Text = "";
- }
- else
- {
- numB = Convert.ToDouble(textBox1.Text);
- textBox1.Text = "";
- }
- }
- //"+" button clicked
- private void button5_Click(object sender, RoutedEventArgs e)
- {
- operatorbox.Text = "+";
- setNum();
- }
- //"-"button clicked
- private void button6_Click(object sender, RoutedEventArgs e)
- {
- operatorbox.Text = "-";
- setNum();
- }
- //"="button clicked
- private void button7_Click(object sender, RoutedEventArgs e)
- {
- setNum();
- getFactory(operatorbox.Text);
- oper = operFactory.createOperator();
- oper.NumberA = numA;
- oper.NumberB = numB;
- textBox1.Text=Convert.ToString( oper.GetResult());
- isNumASet = true;
- }
- private void getFactory(string p)
- {
- switch (p)
- {
- case "+" :
- operFactory = new addFactory();
- break;
- case "-" :
- operFactory = new subFactory();
- break;
- }
- }
- }
36行加入的判断语句很棒(自我感觉良好)
79行的语句也很重要,其次还有另一种实现方法(Green提供)就是将NumA=oper.GetResult();
似乎同样可以达到效果。
至此,简单的加减功能实现,无论你怎么虐待界面都不会跳出错误,而且输入“+=”也能正确得出结果。
嗯 似乎 so far so good
由于使用了工厂模式的设计模式,所以我想扩展其他运算功能似乎不是问题。
现在的问题是“点号”的加入和“清零”的加入,看看明天能实现不 lol