一、使用面向对象的方式实现+、-、* 、/的计算器
•1、找对象
•2、抽象出父类,让子类都有计算能力
•3、实现子类
•4、造工厂
•5、调用
简单工厂模式
父类产品--约束子类
子类产品--实现具体的功能
工厂--生产子类产品
客户--调用者,只需要知道父类产品和工厂
父类
1 abstract class Operation 2 { 3 public Operation() 4 { 5 } 6 public Operation(int numA, int numB) 7 { 8 this.numA = numA; 9 this.numB = numB; 10 } 11 int numA; 12 public int NumA 13 { 14 get { return numA; } 15 set { numA = value; } 16 } 17 18 int numB; 19 public int NumB 20 { 21 get { return numB; } 22 set { numB = value; } 23 } 24 public abstract int JiSuan(); 25 }
子类
1 class Add:Operation 2 { 3 public Add(int numA, int numB) 4 : base(numA, numB) 5 { 6 } 7 8 public override int JiSuan() 9 { 10 return base.NumA + base.NumB; 11 } 12 }
工厂
1 public static Operation GongChang(string type, int numA, int numB) 2 { 3 Operation op = null; 4 switch (type) 5 { 6 case "+": 7 op = new Add(numA, numB); 8 break; 9 case "-": 10 op = new Sub(numA, numB); 11 break; 12 default: 13 throw new Exception("错误"); 14 15 16 } 17 return op; 18 }
窗体 //窗体代码要越少越好
1 //把计算按钮的所有事件都指向次 2 private void bnt_Add_Click_1(object sender, EventArgs e) 3 { 4 JiSuanCilck(sender); 5 } 6 7 private void JiSuanCilck(object sender) 8 { 9 Button b = new Button(); 10 11 if (sender is Button) 12 { 13 b = sender as Button; 14 15 } 16 string text = b.Text; 17 18 Operation op = OperationFactory.GongChang(text, int.Parse(txt_NumA.Text), int.Parse(txt_NumB.Text)); 19 lbl_Result.Text = op.JiSuan().ToString(); 20 }
二、怎么实现多态2-接口
子类继承抽象类,实现接口
接口中的成员必须不能有实现
接口中的成员不能有访问修饰符,隐式公开
接口中可以有属性、方法、索引器等,但不能有字段
接口中的所有成员必须被子类中全部实现
三、计算机升级版,可以制作和Windows自带的计算器一样的(这里只写2个子类,加和减)
1、父类
Operation类代码
Add代码
OperationFactory代码
窗体代码
1 abstract class Operation 2 { 3 public Operation() 4 { 5 } 6 public Operation(int numA, int numB) 7 { 8 this.numA = numA; 9 this.numB = numB; 10 } 11 int numA; 12 public int NumA 13 { 14 get { return numA; } 15 set { numA = value; } 16 } 17 18 int numB; 19 public int NumB 20 { 21 get { return numB; } 22 set { numB = value; } 23 } 24 public abstract int JiSuan(); 25 }
2、子类
1 class Add:Operation 2 { 3 public Add() 4 { } 5 6 public Add(int numA, int numB) 7 : base(numA, numB) 8 { 9 } 10 11 public override int JiSuan() 12 { 13 return base.NumA + base.NumB; 14 } 15 }
3、工厂
1 class OperationFactory 2 { 3 public static Operation GongChang(string type) 4 { 5 Operation op = null; 6 switch (type) 7 { 8 case "+": 9 op = new Add(); 10 break; 11 case "-": 12 op = new Sub(); 13 break; 14 default: 15 throw new Exception("错误"); 16 17 18 } 19 return op; 20 } 21 }
4、窗体
(可以在添加个C“清空键”清空 sum中的值)
1 int numA = 0; 2 int numB = 0; 3 int sum = 0; 4 Operation op = null; 5 6 //把计算按钮的所有事件都指向次 7 private void bnt_Add_Click_1(object sender, EventArgs e) 8 { 9 string text = ""; 10 Button Bu = sender as Button; 11 if (Bu != null) 12 { 13 text = Bu.Text; 14 } 15 numA = int.Parse(txt_NumA.Text); 16 op = OperationFactory.GongChang(text); 17 txt_NumA.Clear(); 18 txt_NumA.Focus(); 19 //JiSuanCilck(sender); 20 } 21 22 private void btn_Result_Click(object sender, EventArgs e) 23 { 24 numB = int.Parse(txt_NumA.Text); 25 op.NumA = numA; 26 op.NumB = numB; 27 sum = op.JiSuan(); 28 txt_NumA.Clear(); 29 txt_NumA.Focus(); 30 txt_NumA.Text = sum.ToString(); 31 32 }