1、攀 枝 花 学 院 实 验 报 告实验课程:Visual C#.NET程序设计教程 实验项目:上机实验5 实验日期:2015.05.05系:数计学院 成绩: 1、 实验目的1、 区分静态类与非静态类,掌握静态字段、静态方法和静态构造函数的定义方法。2、 理解类的继承性与多态性,掌握其应用方法。3、 理解抽象类、接口的概念,掌握抽象类与接口的定义及使用方法。4、 理解分部类和命名空间的概念,掌握分部类和命名空间的使用方法。2、 实验要求1. 熟悉Visual Studio.Net2010的基本操作方法。2. 认真阅读本章相关内容,尤其是案例。3. 实验前进行程序设计,完成源程序的编写任务。4.
2、反复操作,直到不需要参考教材、能熟练操作为止。 3、 实验步骤1、 设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输入当前的学生总人数、该学生的姓名、学生类型和平均成绩。具体要求如下:(1) 每个学生都有的字段为:姓名、年龄。(2) 小学生的字段还有语文、数学,用来表示这两科的成绩。(3) 中学生在此基础上多了英语成绩。(4) 大学生只有必修课和选修课两项成绩。(5) 学生类具有方法来统计自己的总成绩,并输出。(6) 通过静态成员自动记录学生总人数。
3、(7) 成员初始化能通过构造函数完成。源程序如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test3_1 public partial class Form1 : Form public Form1() InitializeComponent(); public ab
4、stract class Student protected string name; protected int age; protected static int number; public Student(string name, int age) this.name = name; this.age = age; number+; public string Name get return name; public virtual string type get return 学生; public abstract double total(); public abstract do
5、uble Average(); public string getInto() string result = string.Format(总人数:0,姓名:1,2,3岁, number, Name, type, age); if (type = 小学生) result += string.Format(,平均成绩为0:N2:n, total() / 2); else if(type=中学生) result += string.Format(,平均成绩为0:N2:n, total() / 3); else result += string.Format(,总学分为0:N2:n, total()
6、; return result; public class Pupil : Student protected double chinese; protected double math; public Pupil(string name, int age, double chinese, double math) : base(name, age) this.chinese = chinese; this.math = math; public override string type get return 小学生; public override double total() return
7、 chinese + math; public class Middle : Student protected double chinese; protected double math; protected double english; public Middle(string name, int age, double chinese, double math,double english) : base(name, age) this.chinese = chinese; this.math = math; this.english = english; public overrid
8、e string type get return 中学生; public override double total() return chinese + math+english; public class University : Student protected double majors; protected double elective; public University(string name, int age, double majors, double elective) : base(name, age) this.majors = majors ; this.elec
9、tive = elective; public override string type get return 大学生; public override double total() return majors+elective; private void btpupil_Click(object sender, EventArgs e) int age = Convert.ToInt32(txtage.Text); double chinese = Convert.ToDouble(txtChinese.Text); double math = Convert.ToDouble(txtMat
10、h.Text); Pupil p = new Pupil(txtname.Text, age, chinese, math); txtshow.Text += p.getInto(); private void btMiddle_Click(object sender, EventArgs e) int age = Convert.ToInt32(txtage.Text); double chinese = Convert.ToDouble(txtChinese.Text); double math = Convert.ToDouble(txtMath.Text); double englis
11、h = Convert.ToDouble(txtEnglish.Text); Middle p = new Middle(txtname.Text, age, chinese, math,english); txtshow.Text += p.getInto(); private void btUniversity_Click(object sender, EventArgs e) int age = Convert.ToInt32(txtAge.Text); double chinese = Convert.ToDouble(txtChinese.Text); double math = C
12、onvert.ToDouble(txtMath.Text); University u = new University(txtName.Text, age, chinese, math); lblshow.Text += u.getInto(); 运行结果如图所示:2、 完善上机实验4-3设计的银行帐户管理系统,增加一个VIP账户的管理。程序功能如下:(1) 当单击“创建VIP账户”按钮时,其中卡号为随机生成的一个在500000到999999之间的值,初始余额为10000元。(2) 在“取款”文本框中输入取款金额后,单击“取款”按钮,如果余额不足,VIP用户可以透支1000元,如取款800,
13、,而余额是400,如透支超过1000元,如取款1600,而余额是400。(3) 其中操作同上机实验4-3。(4) 要求:在上机实验4-3的基础上,通过继承和多态实现上述操作。源程序如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test3_2 public partia
14、l class Form1 : Form public Form1() InitializeComponent(); public class Account protected int cardNo; protected decimal balance; public Account() Random r = new Random(); cardNo = r.Next(100000, 500000); balance = 100; public decimal Balance get return this.balance; public int CardNo get return this
15、.cardNo; public virtual bool GetMoney(decimal money, out string message) if (money = money) balance -= money; message = 操作成功!n取款 + money + 元; return true; else message = 操作失败!n余额不足!; return false; public bool SaveMoney(decimal money, out string message) if (money 0) message = 操作失败!n输入金额不正确!; return
16、false; else balance += money; message = 操作成功!n存款 + money + 元; return true; public class VIPAccount:Account public VIPAccount() Random r = new Random(); cardNo = r.Next(500000, 1000000); balance = 10000; public override bool GetMoney(decimal money, out string message) if (money = money) balance -= mo
17、ney; message = 操作成功!n取款 + money + 元; return true; else if (balance+1000= money) balance -= money; message = 操作成功!n取款 + money + 元,透支+(-balance)+元; return true; else message = 操作失败!?n余额不足!; return false; Account account; private void btVIPAccount_Click(object sender, EventArgs e) account=new VIPAccoun
18、t (); int accountNo=account.CardNo; string message=string.Format(创建VIP账户成功,用户卡号为:0,accountNo); lblShow.Text=n+message +n; private void btAccount_Click(object sender, EventArgs e) account = new VIPAccount(); int accountNo = account.CardNo; string message = String.Format(创建账户成功,用户卡号为:0, account.CardNo
19、); lblShow.Text = n + message + n; private void btGet_Click(object sender, EventArgs e) string message; if (account = null) message = 请先创建账户!; else if (txtGet.Text = ) message = 请输入取款金额!; else decimal money = decimal.Parse(txtGet.Text); account.GetMoney(money, out message); lblShow.Text = n + messag
20、e + n; private void btSave_Click(object sender, EventArgs e) string message; if (account = null) message = 请先创建账户!; else if (txtSave.Text = ) message = 请输入存款金额!; else decimal money = decimal.Parse(txtSave.Text); account.SaveMoney(money, out message); lblShow.Text = n + message + n; private void btBa
21、lance_Click(object sender, EventArgs e) if (account = null) lblShow.Text = 请先创建用户!; else decimal mo = account.Balance; int cardIdNo = account.CardNo; lblShow.Text = 储蓄卡账户:rn + cardIdNo + rn当前余额为a:rn + mo + 元; 运行结果如图所示:3、 声明一个接口IPlayer,包含5个接口方法:播放、停止、暂停、上一首和下一首。设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个AVI播
22、放器类,以实现该接口,最后创建相应类的实例测试程序,单击“MP3”按钮后,再单击“播放”按钮的效果。如果单击“AVI”按钮后,再单击“播放”按钮,则应显示“正在播放AVI视频!”。源程序如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test3_3 public par
23、tial class Form1 : Form public Form1() InitializeComponent(); interface IPlayer string Play(); string Stop(); string Pause(); string Pre(); string Next(); public class MP3 : IPlayer public string Play() return 正在播放MP3歌曲!; public string Stop() return 停止播放MP3歌曲!; public string Pause() return 暂停播放MP3歌曲
24、!; public string Pre() return 播放上一首MP3歌曲!; public string Next() return 播放下一首MP3歌曲!; public class AVI : IPlayer public string Play() return 正在播放AVI歌曲!; public string Stop() return 停止播放AVI歌曲!; public string Pause() return 暂停播放AVI歌曲!; public string Pre() return 播放上一首AVI歌曲!; public string Next() return
25、播放下一首AVI歌曲!; IPlayer ip; MP3 m; AVI a; private void btMP3_Click(object sender, EventArgs e) m = new MP3(); ip = (IPlayer)m; private void btAVI_Click(object sender, EventArgs e) a = new AVI(); ip = (IPlayer)a; private void btPre_Click(object sender, EventArgs e) txtShow.Text = ip.Pre(); private void
26、btStop_Click(object sender, EventArgs e) txtShow.Text = ip.Stop(); private void btPlay_Click(object sender, EventArgs e) txtShow.Text = ip.Play(); private void btPause_Click(object sender, EventArgs e) txtShow.Text = ip.Pause(); private void btNext_Click(object sender, EventArgs e) txtShow.Text = ip.Next(); 运行结果如图所示:四、实验总结- 12 -