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