深入.NET平台和C#编程.第七章:深入理解多态-上机练习2-3

--------------------------------------------Job类--------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Ygzxgzlb
 8 {
 9     /// <summary>
10     /// 父类:工作类
11     /// </summary>
12     public abstract class Job
13     {
14         //工作类型
15         public string Type { get; set; }
16 
17         //工作名称
18         public string Name { get; set; }
19 
20         //描述
21         public string Description { get; set; }
22 
23         //构造函数
24         public Job(string type, string name, string description)
25         {
26             this.Type = type;
27             this.Name = name;
28             this.Description = description;
29         }
30 
31         //方法
32         public Job() { }
33 
34         //执行
35         public abstract void Execute();
36 
37         //执行
38         public abstract void show();
39     }
40 }
Job

--------------------------------------------TestJob类----------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace Ygzxgzlb
 9 {
10     /// <summary>
11     /// 测试工作类
12     /// </summary>
13     public class TestJob:Job
14     {
15         //编写的测试用例个数
16         public int CaseNum { get; set; }
17 
18         //发现的Bugs
19         public int FindBugs { get; set; }
20 
21         //用时
22         public int WorkDay{ get; set; }
23 
24         //构造函数
25         public TestJob(string type , string name , string desc):base(type,name,desc)
26         {
27         }
28 
29         //方法重写
30         public TestJob() { }
31 
32         //实现父类Job的抽象方法Execute(),打开测试任务窗体
33         public override void Execute()
34         {
35             formTestExe te = new formTestExe(this);
36             te.ShowDialog();
37         }
38 
39         //方法重写
40         public override void show()
41         {
42             MessageBox.Show("测试用例个数:" + CaseNum + "
" + "发现的Bug数量:" + FindBugs + "
" + "工作日:" + WorkDay, "指标完成情况", MessageBoxButtons.OK);
43         }
44 
45     }
46 }
TestJob

--------------------------------------------CodeJob类---------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows.Forms;
 7 
 8 namespace Ygzxgzlb
 9 {
10     /// <summary>
11     /// 编码工作类
12     /// </summary>
13     public class CodeJob:Job
14     {
15         //有效编码行数
16         public int CodingLines { get; set; }
17 
18         //目前没有解决的Bug个数
19         public int Bug { get; set; }
20 
21         //用时-工作日
22         public int WorkDay { get; set; }
23 
24          //构造函数
25         public CodeJob(string type, string name, string description)
26         {
27             this.Type = type;
28             this.Name = name;
29             this.Description = description;
30         }
31 
32         //方法重写
33         public CodeJob() { }
34 
35         //实现父类Job的抽象方法Execute(),打开编码工作窗体
36         public override void Execute()
37         {
38             formCodeExe ce = new formCodeExe(this);
39             ce.ShowDialog();
40         }
41 
42         //方法重写
43         public override void show()
44         {
45             MessageBox.Show("有效编码行数:" + CodingLines + "
" + "遗留问题:" + Bug + "
" + "工作日:" + WorkDay, "指标完成情况", MessageBoxButtons.OK);
46         }
47 
48     }
49 }
CodeJob

--------------------------------------------Employee类--------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Ygzxgzlb
 8 {
 9     /// <summary>
10     /// 员工类
11     /// </summary>
12     public  class Employee
13     {
14         //姓名
15         public string Name { get; set; }
16 
17         //年龄
18         public int Age { get; set; }
19 
20         //工号
21         public string Gh { get; set; }
22 
23         //泛型集合
24         public List<Job> WorkList { get; set; }
25 
26         //构造函数
27         public Employee(string name , int age , string gh , List<Job>list )
28         {
29             this.Name = name;
30             this.Gh = gh;
31             this.Age = age;
32             this.WorkList = list;
33         }
34 
35         //方法重写
36         public Employee() { }
37     }
38 }
Employee

--------------------------------------------formMyOffice主窗体-------------------------------

 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 Ygzxgzlb
12 {
13     public partial class formMyOffice : Form
14     {
15         /// <summary>
16         /// 主窗体
17         /// </summary>
18         public formMyOffice()
19         {
20             InitializeComponent();
21         }
22 
23         //定义员工对象
24         Employee empl;
25 
26         //窗口加载
27         private void formMyOffice_Load(object sender, EventArgs e)
28         {
29             Init();
30             UpdateJob();
31             this.groupBox1.Text = empl.Name;
32         }
33 
34         /// <summary>
35         ///初始化某员工工作列表
36         /// </summary>
37         public void Init()
38         {
39             List<Job> joblist = new List<Job>();
40             joblist.Add(new CodeJob("编码", "编码", "实现购物车模块"));
41             joblist.Add(new CodeJob("编码", "编码基类", "完成项目基类编码"));
42             joblist.Add(new TestJob("测试", "压力测试", "测试项目已实现模块"));
43             //实例化员工对象
44             empl = new Employee("王小毛", 18, "10000", joblist);
45         }
46 
47         //绑定工作列表
48         public void UpdateJob()
49         {
50             this.dataGridView1.DataSource = empl.WorkList;
51         }
52 
53         /// <summary>
54         /// 填写执行情况
55         /// </summary>
56         /// <param name="sender"></param>
57         /// <param name="e"></param>
58         private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
59         {
60             int index = this.dataGridView1.CurrentRow.Index;
61             //打开对应窗口,填写完成指标-重写父类的抽象方法Execute()
62             empl.WorkList[index].Execute();
63         }
64 
65         /// <summary>
66         /// 完成情况
67         /// </summary>
68         /// <param name="sender"></param>
69         /// <param name="e"></param>
70         private void 完成情况ToolStripMenuItem_Click(object sender, EventArgs e)
71         {
72             int index = this.dataGridView1.CurrentRow.Index;
73             empl.WorkList[index].show();
74         }
75     }
76 }
formMyOffice

--------------------------------------------formTestExe测试窗体------------------------------

 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 Ygzxgzlb
12 {
13     /// <summary>
14     /// 测试窗口
15     /// </summary>
16     public partial class formTestExe : Form
17     {
18         //测试工作对象
19         TestJob job;
20         public formTestExe(TestJob tj)
21         {
22             InitializeComponent();
23             this.job = tj;
24         }
25 
26         /// <summary>
27         /// 提交测试工作
28         /// </summary>
29         /// <param name="sender"></param>
30         /// <param name="e"></param>
31         private void button1_Click(object sender, EventArgs e)
32         {
33             bool isError = false;
34 
35             try
36             {
37                 job.CaseNum = int.Parse(this.txtcsgs.Text.ToString());
38                 job.FindBugs = int.Parse(this.txtfxgs.Text.ToString());
39                 job.WorkDay = int.Parse(this.txtgzr.Text.ToString());
40             }
41             catch (Exception ex)
42             {
43                 MessageBox.Show(ex.Message);
44                 isError = true;
45             }
46             if (!isError)
47             {
48                 MessageBox.Show("提交成功", "提示");
49                 this.Close();
50             }
51         }
52 
53         /// <summary>
54         /// 取消测试工作
55         /// </summary>
56         /// <param name="sender"></param>
57         /// <param name="e"></param>
58         private void button2_Click(object sender, EventArgs e)
59         {
60             this.Close();
61         }
62     }
63 }
formTestExe

--------------------------------------------formCodeExe编码窗体-----------------------------

 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 Ygzxgzlb
12 {
13     /// <summary>
14     /// 编码窗口
15     /// </summary>
16     public partial class formCodeExe : Form
17     {
18         //编码工作对象
19         CodeJob job;
20         public formCodeExe(CodeJob cj)
21         {
22             InitializeComponent();
23             this.job = cj;
24         }
25 
26         /// <summary>
27         /// 提交编码工作
28         /// </summary>
29         /// <param name="sender"></param>
30         /// <param name="e"></param>
31         private void button1_Click(object sender, EventArgs e)
32         {
33             bool isError = false;
34 
35             try
36             {
37                 job.CodingLines = int.Parse(this.txtbmhs.Text.ToString());
38                 job.Bug = int.Parse(this.txtwt.Text.ToString());
39                 job.WorkDay = int.Parse(this.txtgzr.Text.ToString());
40             }
41             catch (Exception ex)
42             {
43 
44                 MessageBox.Show(ex.Message);
45                 isError = true;
46             }
47             if (!isError)
48             {
49                 MessageBox.Show("提交成功","提示");
50                 this.Close();
51             }
52         }
53 
54         /// <summary>
55         /// 取消编码工作
56         /// </summary>
57         /// <param name="sender"></param>
58         /// <param name="e"></param>
59         private void button2_Click(object sender, EventArgs e)
60         {
61             this.Close();
62         }
63 
64     }
65 }
formCodeExe

--------------------------------------------运行结果-------------------------------------------

原文地址:https://www.cnblogs.com/chenhui666/p/6700957.html