《上机实验3(8页).doc》由会员分享,可在线阅读,更多相关《上机实验3(8页).doc(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、-上机实验3-第 8 页攀枝花学院实验报告实验课程: Visual C#,NET程序设计 实验项目: 上机实验4 实验日期:2015年5月5日 系:数学与计算机学院 班级: 2013级计算机科学与技术 姓名:学号: 同组人: 指导教师:罗明刚 成绩:实验目的:1. 区别静态类与非静态类,掌握静态字段,静态方法和静态构造函数的定义方法.2. 理解类的继承性与多态性,掌握其应用方法.3. 理解抽象类,接口的概念,掌握抽象类与接口的定义及使用方法.4. 理解分部类和命名空间的概念,掌握分部类和命名空间的使用方法.实验仪器设备,药品,器材:Microsoft visual studio 2010实验原
2、理:1.熟悉visual 2010的基本操作方法.1. 认真阅读本章相关内容,尤其是案例.2. 实验前进行程序设计,完成源程序的编写任务.3. 反复操作,直到不需要参考教材,能熟练操作为止.实验步骤:见下页1.设计一个windows应用程序:设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,点击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输入当前的学生总人数,该学生的姓名学生类型和平均成绩。要求如下:(1)每个学生都有的字段为:姓名、年龄(2)小学生的字段还有语文、数学,用来表示这两科的成绩(3)中学
3、生在此基础上多了英语成绩。(4)大学生只有必修课和选修课两项成绩。(5)学生类具有方法来统计自己的总成绩并输出。(6)通过静态成员自动记录学生总人数。(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 _13 public parti
4、al class Form1 : Form public Form1() InitializeComponent(); public abstract class Student protected string name; protected int age; public static int number; public Student(string name, int age) this.name = name; this.age = age; number+; public string Name get return name; public abstract double Ave
5、rage(); public class Pupil : Student protected double math; protected double chinese; public Pupil(string name, int age, double chinese, double math) : base(name, age) this.chinese = chinese; this.math = math; public override double Average() return (chinese + math) / 2; public class Zhongxue : Stud
6、ent protected double math; protected double chinese; protected double english; public Zhongxue(string name, int age, double chinese, double math, double english) : base(name, age) this.chinese = chinese; this.math = math; this.english = english; public override double Average() return (chinese + mat
7、h + english) / 3; public class Daxue : Student protected double bixiu; protected double xuanxiu; public Daxue(string name, int age, double bixiu, double xuanxiu) : base(name, age) this.bixiu = bixiu; this.xuanxiu = xuanxiu; public override double Average() return (bixiu + xuanxiu) / 2; private void
8、button1_Click(object sender, EventArgs e) string a = Convert.ToString(textName.Text); int b = Convert.ToInt32(textAge.Text); double c = Convert.ToDouble(textChinese.Text); double d = Convert.ToDouble(textMath.Text); Pupil p = new Pupil(a, b, c, d); lblshow.Text = 总人数: + Student.number + 姓名: + p.Name
9、 + ,小学生平均成绩为: + p.Average(); private void button2_Click_1(object sender, EventArgs e) string a = Convert.ToString(textName.Text); int b = Convert.ToInt32(textAge.Text); double c = Convert.ToDouble(textChinese.Text); double d = Convert.ToDouble(textMath.Text); double f = Convert.ToDouble(textEnglish.
10、Text); Zhongxue z = new Zhongxue(a, b, c, d, f); lblshow.Text = 总人数: + Student.number + 姓名: + z.Name + ,中学生平均成绩为: + z.Average(); private void button3_Click_1(object sender, EventArgs e) string a = Convert.ToString(textName.Text); int b = Convert.ToInt32(textAge.Text); double c = Convert.ToDouble(tex
11、tChinese.Text); double d = Convert.ToDouble(textMath.Text); Daxue f = new Daxue(a, b, c, d); lblshow.Text = 总人数: + Student.number + 姓名: + f.Name + ,大学生平均成绩为: + f.Average();实验截图:2.完善银行账户管理系统,增加一个VIP账户的管理。程序功能如下:(1)当点击“创建VIP账户”按钮时,显示如图所示信息,其中卡号为随机生成的一个在500000到999999之间的一个值,余额初始化为10000元;(2) 在“取款”文本框中输入取
12、款金额后,点击“取款”按钮,显示如图所示信息。如果余额不足,VIP用户可以透支1000元,如取款800,而余额是400,则显示如图所示信息。如透支超过1000元,如取款1600,而余额是400,则显示如图所示信息.(3) 其中操作同上机实验4-3;(4) 要求:通过继承和多态实现上述操作.实验源代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;
13、using System.Windows.Forms;namespace _16 public partial class Form1 : Form public Form1() InitializeComponent(); Account account; private void button1_Click(object sender, EventArgs e) account = new VIPAccount(); int accountNo = account.CreditNo; string message = string.Format(创建VIP账户成功n用户卡号为:0, acc
14、ountNo); lblshow.Text = n + message + n; private void button2_Click(object sender, EventArgs e) account = new Account(); string message = String.Format(创建户账户成功n用户卡号为:0, account.CreditNo); lblshow.Text = n + message + n; private void button3_Click(object sender, EventArgs e) string message; if (accou
15、nt = null) message = 请先创建账户!; else if (txtq.Text = ) message = 请输入取款余额; else decimal money = decimal.Parse(txtq.Text); account.WithDraw(money, out message); lblshow.Text = n + message + n; lblshow.Text = n + message + n; private void button4_Click(object sender, EventArgs e) string message; if (acco
16、unt = null) message = 请先创建账户!; else if (txtc.Text = ) message = 请输入存款金额; else decimal money = decimal.Parse(txtc.Text); account.CuKuan(money, out message); lblshow.Text = n + message + n; private void button5_Click(object sender, EventArgs e) string message; if (account = null) message = 请先创建账户!; ac
17、count.ChaXun(out message); lblshow.Text = n + message + n;public class Account protected int creditNo; protected decimal banlance; public Account() Random r = new Random(); creditNo = r.Next(100000, 500000); banlance = 100; public int CreditNo get return this.creditNo; public decimal Banlance get re
18、turn this.banlance; public virtual bool WithDraw(decimal money, out string message) if (money = money) banlance -= money; message = 操作成功n取款 + money + 元; return true; else if (banlance + 1000 money) banlance -= money; message = 操作成功n取款 + money + 元,透支 + (-banlance) + 元; return true; else message = 操作失
19、败!n余额不足!; return false; public bool CuKuan(decimal money, out string message) if (money 0) message = 操作失败!n输入金额不正确; return false; else banlance += money; message = 操作成功n存款 + money + 元; return true; public decimal ChaXun(out string message) message = 当前余额为: + banlance + 元; return this.banlance;public
20、 class VIPAccount : Account public VIPAccount() Random r = new Random(); creditNo = r.Next(500000, 1000000); banlance = 10000; public override bool WithDraw(decimal money, out string message) if (money = money) banlance -= money; message = 操作成功n取款 + money + 元; return true; else if (banlance + 1000 m
21、oney) banlance -= money; message = 操作成功n取款 + money + 元,透支 + (-banlance) + 元; return true; else message = 操作失败!n余额不足!; return false;实验截图:3. 声明一个接口Iplayer,包含5个接口方法:播放,停止,暂停,上一首和下一首。在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口。最后创建相应类的实例测试程序,单击MP3按钮后,再单击播放按钮,显示正在播放MP3歌曲!如果单击AVI按钮后,再单击播放按钮 , 则显示正在播放AVI视频!实验源代码:usi
22、ng 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 _17 public partial class Form1 : Form public Form1() InitializeComponent(); interface IPlayer string Play(); string S
23、top(); 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歌曲!; public string Pre() return 播放上一首MP3歌曲!; public string Next() return 播放下一首MP3歌曲!; public class AVI :
24、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 播放下一部AVI视频!; IPlayer ip; MP3 m; AVI a; private void button1_Click(object sender, EventArgs e) m = new M
25、P3(); ip = (IPlayer)m; private void button7_Click(object sender, EventArgs e) a = new AVI(); ip = (IPlayer)a; private void button2_Click(object sender, EventArgs e) lblshow.Text = ip.Pre(); private void button3_Click(object sender, EventArgs e) lblshow.Text = ip.Stop(); private void button4_Click(object sender, EventArgs e) lblshow.Text = ip.Play(); private void button5_Click(object sender, EventArgs e) lblshow.Text = ip.Pause(); private void button6_Click(object sender, EventArgs e) lblshow.Text = ip.Next();实验截图:4.实验总结: