(深入.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 Calc.entity
 8 {
 9     public class Op
10     {
11         public double No1 { get; set; }
12         public double No2 { get; set; }
13         public virtual double GetResult()
14         {
15             double result = 0;
16             return result;
17         }
18     }
19 }
Op

---------------------------------------------------子类(加)---------------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Calc.entity
 8 {
 9     public class Jia :Op
10     {
11 
12         public override double GetResult()
13         {
14             double result = base.No1 + base.No2;
15             return result;
16         }
17     }
18 }
Jia

---------------------------------------------------子类(减)---------------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Calc.entity
 8 {
 9     public class Jian:Op
10     {
11         public override double GetResult()
12         {
13             double result = base.No1 - base.No2;
14             return result;
15         }
16     }
17 }
Jian

---------------------------------------------------子类(乘)---------------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Calc.entity
 8 {
 9     public class Cheng:Op
10     {
11         public override double GetResult()
12         {
13             double result = base.No1 * base.No2;
14             return result;
15         }
16     }
17 }
Cheng

---------------------------------------------------子类(除)---------------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Calc.entity
 8 {
 9     public class Chu : Op
10     {
11         public override double GetResult()
12         {
13             double result = base.No1 / base.No2;
14             return result;
15         }
16     }
17 }
Chu

---------------------------------------------------窗体类---------------------------------------------------

 1 using Calc.entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 
12 namespace Calc
13 {
14     public partial class frmCalc : Form
15     {
16         public frmCalc()
17         {
18             InitializeComponent();
19         }
20 
21         private void btnCalc_Click(object sender, EventArgs e)
22         {
23             Op op = new Op();
24             if (cmbFu.Text.Equals("+"))
25             {
26                 op = new Jia();
27             }
28             if (cmbFu.Text.Equals("-"))
29             {
30                 op = new Jian();
31             }
32             if (cmbFu.Text.Equals("*"))
33             {
34                 op = new Cheng();
35             }
36             if (cmbFu.Text.Equals("/"))
37             {
38                 op = new Chu();
39             }
40             if (string.IsNullOrEmpty(txtNo1.Text.Trim()) || string.IsNullOrEmpty(txtNo2.Text.Trim()))
41             {
42                 MessageBox.Show("不能为空!!!");
43                 return;
44             }
45             op.No1 = double.Parse(txtNo1.Text);
46             op.No2 = double.Parse(txtNo2.Text);
47             lblResult.Text = op.GetResult().ToString();
48         }
49 
50         private void frmCalc_Load(object sender, EventArgs e)
51         {
52             cmbFu.Items.Add("+");
53             cmbFu.Items.Add("-");
54             cmbFu.Items.Add("*");
55             cmbFu.Items.Add("/");
56         }
57     }
58 }
frmCalc.cs
原文地址:https://www.cnblogs.com/1-2-3-4/p/6691437.html