攀枝花学院实验报告
实验课程:Visual C#.NET程序设计教程 实验项目:上机实验5 实验日期:2015.05.05
系:数计学院 成绩:
一、实验目的
1、区分静态类与非静态类,掌握静态字段、静态方法和静态构造函数的定义方法。
2、理解类的继承性与多态性,掌握其应用方法。
3、理解抽象类、接口的概念,掌握抽象类与接口的定义及使用方法。
4、理解分部类和命名空间的概念,掌握分部类和命名空间的使用方法。
二、实验要求
1.熟悉Visual Studio.Net2010的基本操作方法。
2.认真阅读本章相关内容,尤其是案例。
3.实验前进行程序设计,完成源程序的编写任务。
4.反复操作,直到不需要参考教材、能熟练操作为止。
三、实验步骤
1、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输入当前的学生总人数、该学生的姓名、学生类型和平均成绩。具体要求如下:
(1)每个学生都有的字段为:姓名、年龄。
(2)小学生的字段还有语文、数学,用来表示这两科的成绩。
(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 Test3_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public abstract 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 double 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());
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 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 override 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.elective = 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(txtMath.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 english = 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 = Convert.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,,而余额是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 partial 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.cardNo; }
}
public virtual bool GetMoney(decimal money, out string message)
{
if (money < 0)
{
message = "操作失败!\n输入金额不正确!";
return false;
}
else if (balance >= 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 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 < 0)
{
message = "操作失败!\n输入金额不正确!";
return false;
}
else if (balance >= money)
{
balance -= money;
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 VIPAccount ();
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);
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" + message + "\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 btBalance_Click(object sender, EventArgs e)
{
if (account == null)
{
lblShow.Text = "请先创建用户!";
}
else
{
decimal mo = account.Balance;
int cardIdNo = account.CardNo;
lblShow.Text = "储蓄卡账户:\r\n" + cardIdNo + "\r\n当前余额为a:\r\n" + mo + "元";
}
}
}
}
运行结果如图所示:
3、声明一个接口IPlayer,包含5个接口方法:播放、停止、暂停、上一首和下一首。设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口,最后创建相应类的实例测试程序,单击“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 partial 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歌曲!";
}
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 "播放下一首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 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();
}
}
}
运行结果如图所示:
四、实验总结
实验名称:粉体真密度的测定粉体真密度是粉体质量与其真体积之比值,其真体积不包括存在于粉体颗粒内部的封闭空洞。所以,测定粉体的真密度…
研究生实验报告范本实验课程实验名称实验地点学生姓名学号指导教师范本实验时间年月日一实验目的熟悉电阻型气体传感器结构及工作原理进行基…
实验报告课程名称物证技术学实验项目名称捺印手印样本指纹显现提取班级与班级代码074213070853实验室名称或课室法学实验教学中…
滁州市政府组织退耕还林(黑体,小二,1.5倍行距,段前段后0.5行)——5060451007范雪花(学号,TimesNewRoma…
科学实验报告单1科学实验报告单2科学实验报告单3科学实验报告单4九完小科学实验报告单10九完小科学实验报告单11九完小科学实验报告…
滁州市政府组织退耕还林(黑体,小二,1.5倍行距,段前段后0.5行)——5060451007范雪花(学号,TimesNewRoma…
实验名称:粉体真密度的测定粉体真密度是粉体质量与其真体积之比值,其真体积不包括存在于粉体颗粒内部的封闭空洞。所以,测定粉体的真密度…
研究生实验报告范本实验课程实验名称实验地点学生姓名学号指导教师范本实验时间年月日一实验目的熟悉电阻型气体传感器结构及工作原理进行基…
2020学年第学期佛山职业技术学院食品营养与检测专业实验报告课程名称食品添加剂专业班级姓名学号所在组别同组成员实验地点实验时间实验…
运筹学课程实验报告实验名称实验三灵敏性报告与运输规划任课教师专业班级姓名学号完成日期一实验目的1熟悉LINGO的模型结果报告的主要…