------------------------------------------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 Employment 8 { 9 public class Employee10 {11 //年龄12 public int Age { get; set; }13 14 //性别15 public Gender Gender { get; set; }16 17 //工号18 public string ID { get; set; }19 20 //姓名21 public string Name { get; set; }22 23 //给Employee类添加工作列表属性24 protected ListWorkList { get; set; }25 26 //构造函数27 public Employee(string id , int age , Gender gender , string name , List list)28 {29 this.Age = age;30 this.Gender = gender;31 this.ID = id;32 this.Name = name;33 this.WorkList = list;34 }35 36 //虚方法37 public virtual string SayHi()38 {39 string ss = this.Name;40 return ss;41 }42 43 44 }45 }
------------------------------------------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 Employment12 {13 public partial class formMyOffice : Form14 {15 public formMyOffice()16 {17 InitializeComponent();18 }19 20 //采用泛型集合List保存内容21 Listempls = new List ();22 23 /// 24 /// 窗口加载25 /// 26 /// 27 /// 28 private void formMyOffice_Load(object sender, EventArgs e)29 {30 Listlist1 = new List ();31 list1.Add(new Job("编码", "购物车模块"));32 list1.Add(new Job("测试","给购物车模块做单元测试"));33 SE ai = new SE("112" , "刚刚" , 25 , Gender.男 , 100 , list1);34 35 List list2 = new List ();36 list2.Add(new Job("设计", "数据库模块"));37 list2.Add(new Job("编写文档", "详细设计说明书"));38 SE joe = new SE("113", "菲菲", 22, Gender.女, 200, list2);39 40 PM pm = new PM("890" , "康康" , 30 , Gender.男 , 50 , null);41 empls.Add(ai);42 empls.Add(joe);43 empls.Add(pm);44 }45 46 /// 47 /// 员工汇报工作48 /// 49 /// 50 /// 51 private void button1_Click(object sender, EventArgs e)52 {53 54 foreach (Employee item in empls)55 {56 57 MessageBox.Show(item.SayHi());58 //if (item is PM)59 //{60 // MessageBox.Show(((PM)item).DoWork(),"汇报");61 //}62 //if (item is SE)63 //{64 // MessageBox.Show(((SE)item).DoWork(),"汇报");65 //}66 }67 }68 69 70 71 }72 }
------------------------------------------Gender类---------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Employment 8 { 9 //枚举性别10 public enum Gender11 {12 男,13 女14 }15 }
------------------------------------------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 Employment 8 { 9 public class Job10 {11 //工作名称12 public string Name { get; set; }13 14 //描述15 public string Description { get; set; }16 17 //构造函数18 public Job(string name , string description)19 {20 this.Name = name;21 this.Description = description;22 }23 24 25 }26 }
------------------------------------------PM类--------------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Employment 8 { 9 public class PM:Employee10 {11 //属性12 public int YearOfExperience { get; set; }13 14 //虚方法15 public override string SayHi()16 {17 string message = this.Name + ":管理员工完成工作内容!";18 return message;19 }20 21 //修改PM类的构造函数22 public PM(string id , string name , int age , Gender gender , int yearOfExperience , Listlist):base(id , age , gender , name , list)23 {24 this.YearOfExperience = yearOfExperience;25 }26 27 28 }29 }
------------------------------------------SE类--------------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Employment 8 { 9 public class SE:Employee10 {11 //属性12 public int Popularity { get; set; }13 14 //虚方法15 public override string SayHi()16 {17 StringBuilder sb = new StringBuilder();18 sb.Append(this.Name + ":\n");19 for (int i = 0; i < this.WorkList.Count; i++)20 {21 sb.Append((i + 1) + "," + WorkList[i].Name + ":" + WorkList[i].Description + "\n");22 }23 return sb.ToString();24 }25 26 //修改SM类的构造函数27 public SE(string id , string name , int age , Gender gender , int popularity , Listlist):base(id , age , gender , name , list)28 {29 this.Popularity = popularity;30 }31 32 33 }34 }
------------------------------------------运行结果-----------------------------------------------