(深入.Net平台和C#编程)第六章.上机练习4.20170410

  ----------父类----------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Lesson6_4
 8 {
 9     /// <summary>
10     /// 父类
11     /// </summary>
12     public class Operation
13     {
14         /// <summary>
15         /// 添加属性
16         /// </summary>
17         public double NumberA { get; set; }
18 
19         public double NumberB { get; set; }
20 
21      
22 
23         /// <summary>
24         /// 定义虚方法GenResult(),返回类型为double
25         /// </summary>
26         /// <returns></returns>
27         public virtual double GetResult()
28         {
29             double result = 0;
30             return result;
31         }
32     }
33 }
View Code

  ----------OperationAdd类----------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Lesson6_4
 8 {
 9     /// <summary>
10     /// 实现加法类
11     /// </summary>
12     public class OperationAdd:Operation//调用父类
13     {
14         public override double GetResult()
15         {
16             double result = NumberA + NumberB;
17             return result;
18         }
19         
20     }
21 
22     /// <summary>
23     /// 实现减法类
24     /// </summary>
25     public class OperationAn : Operation//调用父类
26     {
27         public override double GetResult()
28         {
29             double result = NumberA - NumberB;
30             return result;
31         }
32     }
33 
34     /// <summary>
35     /// 实现除法的类
36     /// </summary>
37     public class OperationDiv : Operation
38     {
39         public override double GetResult()
40         {
41             if (NumberB == 0)
42             {
43                 throw new Exception("除数不能为0!");
44             }
45             double result = NumberA / NumberB;
46             return result;
47         }
48     }
49 
50     /// 实现乘法的类
51     /// </summary>
52     public class OperationCheng : Operation
53     {
54         public override double GetResult()
55         {
56             if (NumberB == 0)
57             {
58                 throw new Exception("除数不能为0!");
59             }
60             double result = NumberA * NumberB;
61             return result;
62         }
63     }
64 }
View Code

  ----------FrmCalc类----------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace Lesson6_4
12 {
13     public partial class FrmCalc : Form
14     {
15         public FrmCalc()
16         {
17             InitializeComponent();
18         }
19 
20         /// <summary>
21         /// 单击"计算"按钮的响应
22         /// </summary>
23         /// <param name="sender"></param>
24         /// <param name="e"></param>
25         private void button1_Click(object sender, EventArgs e)
26         {
27             //判断文本是否为空
28             if (string.IsNullOrEmpty(txtOne.Text.Trim()) || string.IsNullOrEmpty(txtTwo.Text.Trim()))
29             {
30                 MessageBox.Show("操作数不能为空!", "提示");
31                 //设置焦点
32                 this.txtOne.Focus();
33                 this.txtTwo.Focus();
34                 return;
35             }
36             else
37             {
38                 //设置符号
39                 try
40                 {
41                     Operation opr = new Operation();
42                     switch (this.cmdOper.SelectedItem.ToString().Trim())
43                     {
44                         case "+":
45                             {
46                                 opr = new OperationAdd();
47                                 break;
48                             }
49                         case "-":
50                             {
51                                 opr = new OperationAn();
52                                 break;
53                             }
54                         case "*":
55                             {
56                                 opr = new OperationCheng();
57                                 break;
58                             }
59                         case "/":
60                             {
61                                 opr = new OperationDiv();
62                                 break;
63                             }
64                     }
65                     //计算参与计算的数据
66                     opr.NumberA = double.Parse(this.txtOne.Text.Trim());
67                     opr.NumberB = double.Parse(this.txtTwo.Text.Trim());
68                     //计算
69                     this.lbResult.Text = opr.GetResult().ToString();
70                     this.lbResult.Visible = true;
71                 }
72                 catch (Exception ex)
73                 {
74 
75                     MessageBox.Show("发生错误!" + ex.Message);
76                 }
77             }
78         }
79 
80         /// <summary>
81         /// 设置组合框默认选中
82         /// </summary>
83         /// <param name="sender"></param>
84         /// <param name="e"></param>
85         private void FrmCalc_Load(object sender, EventArgs e)
86         {
87             cmdOper.SelectedIndex = 0;
88         }
89     }
90 }
View Code
原文地址:https://www.cnblogs.com/weiai520/p/6691784.html