1 public class SE:Employee 2 { 3 private int popularity; 4 public int Popularity 5 { 6 get { return popularity; } 7 set { popularity = value; } 8 } 9 10 public string DoWork() 11 { 12 StringBuilder sb = new StringBuilder(); 13 sb.Append(this.Name + ": "); 14 for (int i = 0; i < this.workList.Count; i++) 15 { 16 sb.Append((i + 1) + "、" + workList[i].Name + ":" + 17 workList[i].Description + " "); 18 19 } 20 return sb.ToString(); 21 } 22 public SE(string id, string name, int age, Gender gender, int popularity, 23 List<Job>list):base(id,name,age,gender,list) 24 { 25 this.Popularity = popularity; 26 }
1 public class PM:Employee 2 { 3 public int YearOfExperience { get; set; } 4 public string Dowork() 5 { 6 string message = this.Name + ":管理员工完成工作内容!"; 7 return message; 8 } 9 public PM(string id, string name, int age, Gender gender, int yearOfExperience, 10 List<Job> list):base(id,name,age,gender,list) 11 12 { 13 this.YearOfExperience = YearOfExperience; 14
1 public class Job//定义工作项目 2 { 3 public string Name { get; set; }//工作名称 4 public string Description { get; set; }//描述 5 //构造函数 6 public Job(string name, string description) 7 { 8 this.Name = name; 9 this.Description = description; 10 } 11 }
1 public enum Gender//枚举 2 { 3 male, female 4 5 }
public partial class Form1 : Form { List<Employee> empls = new List<Employee>(); public Form1() { InitializeComponent(); Init(); } public void Init() { List<Job> list1 = new List<Job>(); list1.Add(new Job("编码", "购物车模块")); list1.Add(new Job("测试", "给购物车模块做单元测试")); SE ai = new SE("112", "艾边成", 25, Gender.male, 100, list1); List<Job> list2 = new List<Job>(); list2.Add(new Job("设计", "数据库建模")); list2.Add(new Job("编写文档", "详细设计说明书")); SE job = new SE("113", "Joe", 24,Gender.female, 200, list2); PM pm = new PM("890", "比尔", 50, Gender.female, 30, null); empls.Add(ai); empls.Add(job); empls.Add(pm); } private void button1_Click(object sender, EventArgs e) { foreach (Employee emp in empls) { if (emp is PM) { MessageBox.Show(((PM)emp).Dowork(), "汇报"); } if (emp is SE) { MessageBox.Show(((SE)emp).DoWork(), "汇报"); } } }
1 public class Employee 2 { 3 public int Age { get; set; } 4 public Gender Sex { get; set; } 5 public string ID { get; set; } 6 public string Name { get; set; } 7 //给Employee添加工作列表属性 8 protected List<Job> workList { get; set; } 9 //构造 10 public Employee(string id, string name, int age, Gender gender, List<Job> list) 11 { 12 this.Age = age; 13 this.ID = id; 14 this.Name = name; 15 this.Sex = gender; 16 this.workList = list; 17 18 }