首页 > 代码库 > 第七章.上机练习1
第七章.上机练习1
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace Lesson7.Entity 9 { 10 /// <summary> 11 /// 萨克斯类 12 /// </summary> 13 public class Celanese:Instrument 14 { 15 /// <summary> 16 /// 演奏方法 17 /// </summary> 18 public override void Play() 19 { 20 MessageBox.Show("萨克斯在演奏"); 21 } 22 } 23 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson7.Entity 8 { 9 /// <summary> 10 /// 员工类 11 /// </summary> 12 public class Employee 13 { 14 //年龄 15 public int Age { get; set; } 16 //性别 17 public Gender Gender { get; set; } 18 //工号 19 public string ID { get; set; } 20 //姓名 21 public string Name { get; set; } 22 //无参构造函数 23 public Employee() 24 { 25 26 } 27 //有参构造函数 28 public Employee( string id, string name) 29 { 30 this.ID = id; 31 this.Name = name; 32 33 } 34 } 35 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson7.Entity 8 { 9 /// <summary> 10 /// 乐器类 11 /// </summary> 12 public class Instrument 13 { 14 /// <summary> 15 /// 演奏方法 16 /// </summary> 17 public virtual void Play() 18 { 19 Console.WriteLine("演奏"); 20 } 21 } 22 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace Lesson7.Entity 9 { 10 /// <summary> 11 /// 钢琴类 12 /// </summary> 13 public class Piano:Instrument 14 { 15 public override void Play() 16 { 17 MessageBox.Show("钢琴在演奏"); 18 } 19 } 20 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson7.Entity 8 { 9 /// <summary> 10 /// 项目经理类 11 /// </summary> 12 public class PM : Employee 13 { 14 //项目经验 15 private int _YearOfExperience; 16 public int YearOfExperience 17 { 18 get { return _YearOfExperience; } 19 set { _YearOfExperience = value; } 20 } 21 public PM(string id, string name, int yearOfExperience) 22 : base(id, name) 23 { 24 this._YearOfExperience = yearOfExperience; 25 } 26 27 } 28 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace Lesson7.Entity 9 { 10 /// <summary> 11 /// 小提琴类 12 /// </summary> 13 public class Violin:Instrument 14 { 15 /// <summary> 16 /// 演奏方法 17 /// </summary> 18 public override void Play() 19 { 20 MessageBox.Show("小提琴在演奏"); 21 } 22 } 23 }
1 using Lesson7.Entity; 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 12 namespace Lesson7 13 { 14 public partial class EngineerForm : Form 15 { 16 public List<SE> ses = new List<SE>(); 17 public EngineerForm() 18 { 19 InitializeComponent(); 20 } 21 /// <summary> 22 /// 初始员工 23 /// </summary> 24 public void IniSE() 25 { 26 SE se = new SE("001", "王小毛", 100); 27 SE se1 = new SE("002", "周新宇", 300); 28 SE se2 = new SE("003", "盖茨", 300); 29 ses.Add(se); 30 ses.Add(se1); 31 ses.Add(se2); 32 dgvList.DataSource = ses.ToList(); 33 } 34 /// <summary> 35 /// 窗体加载事件 36 /// </summary> 37 /// <param name="sender"></param> 38 /// <param name="e"></param> 39 private void EngineerForm_Load(object sender, EventArgs e) 40 { 41 dgvList.AutoGenerateColumns = false; 42 IniSE(); 43 } 44 /// <summary> 45 /// 演奏按钮 46 /// </summary> 47 /// <param name="sender"></param> 48 /// <param name="e"></param> 49 private void tsmiIns_Click(object sender, EventArgs e) 50 { 51 if (dgvList.SelectedRows.Count > 0) 52 { 53 foreach (var item in ses) 54 { 55 if (item.ID.Equals(dgvList.SelectedRows[0].Cells[0].Value.ToString())) 56 { 57 PlayForm play = new PlayForm(); 58 play.from = this; 59 play.index = dgvList.CurrentRow.Index; 60 play.Show(); 61 } 62 } 63 } 64 else 65 { 66 MessageBox.Show("请选择员工"); 67 } 68 } 69 70 } 71 }
1 using Lesson7.Entity; 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 12 namespace Lesson7 13 { 14 public partial class PlayForm : Form 15 { 16 public PlayForm() 17 { 18 InitializeComponent(); 19 20 } 21 public EngineerForm from; 22 public int index; 23 /// <summary> 24 /// 窗体加载事件 25 /// </summary> 26 /// <param name="sender"></param> 27 /// <param name="e"></param> 28 private void PlayForm_Load(object sender, EventArgs e) 29 { 30 gbName.Text = from.ses[index].Name; 31 } 32 /// <summary> 33 /// 演奏单击按钮 34 /// </summary> 35 /// <param name="sender"></param> 36 /// <param name="e"></param> 37 private void btnPlay_Click(object sender, EventArgs e) 38 { 39 SE se = new SE(); 40 Instrument ins = new Instrument(); 41 if (rdbCelanese.Checked == true) 42 { 43 ins = new Celanese(); 44 } 45 else if (rdbPiano.Checked == true) 46 { 47 ins = new Piano(); 48 } 49 else 50 { 51 ins = new Violin(); 52 } 53 se.Play(ins); 54 } 55 } 56 }
第七章.上机练习1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。