深入.NET平台和C#编程.第五章:上机练习2 - 3

------------------------------------------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 Employee
10     {
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 List<Job> WorkList { get; set; }
25 
26         //构造函数
27         public Employee(string id , int age , Gender gender , string name , List<Job> 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 }
View Code

------------------------------------------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 Employment
12 {
13     public partial class formMyOffice : Form
14     {
15         public formMyOffice()
16         {
17             InitializeComponent();
18         }
19 
20         //采用泛型集合List保存内容
21         List<Employee> empls = new List<Employee>();
22 
23         /// <summary>
24         /// 窗口加载
25         /// </summary>
26         /// <param name="sender"></param>
27         /// <param name="e"></param>
28         private void formMyOffice_Load(object sender, EventArgs e)
29         {
30             List<Job> list1 = new List<Job>();
31             list1.Add(new Job("编码", "购物车模块"));
32             list1.Add(new Job("测试","给购物车模块做单元测试"));
33             SE ai = new SE("112" , "刚刚" , 25 , Gender.男 , 100 , list1);
34 
35             List<Job> list2 = new List<Job>();
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         /// <summary>
47         /// 员工汇报工作
48         /// </summary>
49         /// <param name="sender"></param>
50         /// <param name="e"></param>
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 }
View Code

------------------------------------------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 Gender
11     {
12         男,
13 14     }
15 }
View Code

------------------------------------------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 Job
10     {
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 }
View Code

------------------------------------------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:Employee
10     {
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 , List<Job> list):base(id , age , gender , name , list)
23         {
24             this.YearOfExperience = yearOfExperience;
25         }
26 
27 
28     }
29 }
View Code

------------------------------------------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:Employee
10     {
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 , List<Job> list):base(id , age , gender , name , list)
28         {
29             this.Popularity = popularity;
30         }
31 
32 
33     }
34 }
View Code

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

posted @ 2017-04-11 02:33  吻你,佑脸&辉  阅读(1081)  评论(0编辑  收藏  举报