首页 > 代码库 > (深入.Net平台和C#编程)第六章上机练习3.李向阳.20170411

(深入.Net平台和C#编程)第六章上机练习3.李向阳.20170411

技术分享
 1 =======父类==========
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj3.Entity
 9 {
10     /// <summary>
11     /// 父类
12     /// </summary>
13     public class Employee
14     {
15         public string ID { get; set; }
16         public string Name { get; set; }
17         public int Age { get; set; }
18         public List<Job> WorkList { get; set; }
19         public Gender Sex { get; set; }
20         /// <summary>
21         /// 构造函数
22         /// </summary>
23         /// <param name="id"></param>
24         /// <param name="name"></param>
25         /// <param name="age"></param>
26         /// <param name="worklist"></param>
27         /// <param name="sex"></param>
28         public Employee(string id, string name, int age, List<Job> worklist, Gender sex)
29         {
30             this.ID = id;
31             this.Name = name;
32             this.Age = age;
33             this.Sex = sex;
34             this.WorkList = worklist;
35         }
36 
37         public virtual string DoWork()
38         {
39             return "";
40         }
41     }
42 }
View Code
技术分享
 1 =======性别类==========
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj3.Entity
 9 {
10     /// <summary>
11     /// 性别
12     /// </summary>
13     public enum Gender
14     {
15         男,女
16     }
17 }
View Code
技术分享
 1 =========Job类=========
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj3.Entity
 9 {
10     /// <summary>
11     /// 定义工作项类
12     /// </summary>
13     public class Job
14     {
15         public string Description { get; set; }  //描述
16         public string Name { get; set; }   //工作名称
17         /// <summary>
18         /// 构造函数
19         /// </summary>
20         public Job(string description, string name)
21         {
22             this.Description = description;
23             this.Name = name;
24         }
25     }
26 }
View Code
技术分享
 1 =======子PM类===============
 2 
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace Sj3.Entity
10 {
11     /// <summary>
12     /// PM类
13     /// </summary>
14     public class PM : Employee
15     {
16 
17         public PM(string name, string id, int age, Gender sex, List<Job> worklist)
18             : base(id, name, age, worklist, sex)
19         { }
20 
21         /// <summary>
22         /// 给PM类添加DoWork()方法:工作
23         /// </summary>
24         public override string DoWork()
25         {
26             string mes = this.Name + ":管理员工完成工作内容!";
27             return mes;
28         }
29     }
30 }
View Code
技术分享
 1 ===========子SE类================
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj3.Entity
 9 {
10     /// <summary>
11     /// SE类
12     /// </summary>
13     public class SE : Employee
14     {
15         public SE(string name, string id, int age, Gender sex, List<Job> worklist)
16             : base(id, name, age, worklist, sex)
17         { }
18         /// <summary>
19         /// 给SE类添加DoWork()方法:工作
20         /// </summary>
21         public override string DoWork()
22         {
23             StringBuilder sdd = new StringBuilder();
24             sdd.Append(this.Name + ":\n");
25             for (int i = 0; i < this.WorkList.Count; i++)
26             {
27                 sdd.Append((i + 1) + "," + WorkList[i].Name + ":" + WorkList[i].Description + "\n");
28 
29             }
30             return sdd.ToString();
31         }
32     }
33 }
View Code
技术分享
 1 =============测试类==================
 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 using Sj3.Entity;
12 
13 
14 namespace Sj3
15 {
16     public partial class FrmMain : Form
17     {
18         public FrmMain()
19         {
20             InitializeComponent();
21             InfoMain();
22         }
23         List<Employee> emply = new List<Employee>();
24         /// <summary>
25         /// 初始化员工信息
26         /// </summary>
27         public void InfoMain()
28         {
29             //实例SE对象
30             List<Job> job = new List<Job>();
31             job.Add(new Job("购物车模块", "编码"));
32             job.Add(new Job("给购物车模块做单元测试", "测试"));
33             SE se = new SE("爱边成", "111", 25, Gender.男, job);
34 
35             //实例SE对象
36             List<Job> job1 = new List<Job>();
37             job1.Add(new Job("购物车模块", "编码"));
38             job1.Add(new Job("给购物车模块做单元测试", "测试"));
39             SE se1 = new SE("joe", "112", 30, Gender.男, job1);
40 
41             //实例化PM对象
42             PM pm = new PM("盖茨", "114", 50, Gender.男, job1);
43             emply.Add(se);
44             emply.Add(se1);
45             emply.Add(pm);
46             Console.Read();
47         }
48         /// <summary>
49         ///汇报工作 
50         /// </summary>
51         /// <param name="sender"></param>
52         /// <param name="e"></param>
53         private void button1_Click(object sender, EventArgs e)
54         {
55             foreach (Employee emp in emply)
56             {
57                 //if (emp is PM)
58                 //{
59                 //    MessageBox.Show(((PM)emp).DoWork(), "汇报");
60                 //}
61                 //if (emp is SE)
62                 //{
63                 //    MessageBox.Show(((SE)emp).DoWork(), "汇报");
64                 MessageBox.Show(emp.DoWork());
65             }
66         }
67     }
68 }
View Code

效果图:

技术分享

(深入.Net平台和C#编程)第六章上机练习3.李向阳.20170411