实验—面向对象的高程序设计.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date实验面向对象的高程序设计数据库原理实验报告实验5面向对象的高级程序设计实验日期和时间:实验室:班级: 学号:姓名: 实验环境:1. 硬件:主频:2.20 GHz内存:海力士 DDR3 1600 MHz 2GB硬盘空间:500GB 5400转/分操作系统版本:win72. 软件:Microsoft Visual Studio 2010实验主要任务:(1) 设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输入当前的学生人数、该学生的姓名、学生类型和平均成绩。要求如下:每个学生都有姓名和年龄。小学生有语文、数学成绩。中学生有语文、数学和英语成绩。大学生有必修课学分总数和选修课学分总数,不包含单科成绩。学生类提供向外输出信息的方法。学生类提供统计个人总成绩或总学分的方法。通过静态成员自动记录学生的总人数。能通过构造函数完成各字段成员初始化。(2) 设计一个Windows应用程序,在该程序中定义平面图形抽象类和其派生类圆、矩形和三角形。该程序实现的功能包括:输入相应的图形的参数,如矩形的长和宽,单击相应的按钮,根据输入参数创建图形类并输出该图形的面积。(3) 声明一个播放器接口IPlayer,包含5个接口方法:播放、停止、暂停、上一首和下一首。设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口,最后创建相应类的实例测试程序。以下内容填写请利用截屏图片和文字对实验原理和实验效果进行说明任务1( )完成情况:实际效果如下:代码: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 S5_1 public partial 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 virtual string type get return "学生" public abstract double total(); public string getInfo() 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 button1_Click(object sender, EventArgs e) int age = Convert.ToInt32(textBox2.Text); double sub1 = Convert.ToDouble(textBox3.Text); double sub2 = Convert.ToDouble(textBox4.Text); Pupil p = new Pupil(textBox1.Text, age, sub1, sub2); label6.Text += p.getInfo(); private void button2_Click(object sender, EventArgs e) int age = Convert.ToInt32(textBox2.Text); double sub1 = Convert.ToDouble(textBox3.Text); double sub2 = Convert.ToDouble(textBox4.Text); double sub3 = Convert.ToDouble(textBox5.Text); Middle m = new Middle (textBox1.Text, age, sub1, sub2,sub3); label6.Text += m.getInfo(); private void button3_Click(object sender, EventArgs e) int age = Convert.ToInt32(textBox2.Text); double sub1 = Convert.ToDouble(textBox3.Text); double sub2 = Convert.ToDouble(textBox4.Text); University u = new University(textBox1.Text, age, sub1, sub2); label6.Text += u.getInfo(); 运行结果:自我评价:达到了实验预计的效果 任务2( )完成情况:实际效果如下:代码: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 S5_2 public partial class Form1 : Form public Form1() InitializeComponent(); public abstract class Figure public abstract double Area(); public class Circle : Figure double radius; public Circle(double r) radius = r; public override double Area() return radius * radius * 3.14; public class Rectangle : Figure double length; double width; public Rectangle(double l ,double w) length = l; width = w; public override double Area() return length * width; public class Triangle : Figure double bottom; double high; public Triangle(double b, double h) bottom = b; high = h; public override double Area() return bottom * high / 2; private void button1_Click(object sender, EventArgs e) double r = Convert.ToDouble(textBox1.Text); Circle a = new Circle(r); label6.Text = "圆的面积为:"+ a.Area(); private void button2_Click(object sender, EventArgs e) double l = Convert.ToDouble(textBox1.Text); double w = Convert.ToDouble(textBox2.Text); Rectangle a = new Rectangle(l,w); label6.Text = "矩形面积为:" + a.Area(); private void button3_Click(object sender, EventArgs e) double b = Convert.ToDouble(textBox1.Text); double h = Convert.ToDouble(textBox2.Text); Triangle a = new Triangle(b,h); label6.Text = "三角形面积为:" + a.Area(); 运行结果:自我评价:达到了实验预计的效果 任务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 S5_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 button1_Click(object sender, EventArgs e) m = new MP3(); ip = (IPlayer)m; private void button2_Click(object sender, EventArgs e) a = new AVI(); ip = (IPlayer)a; private void button3_Click(object sender, EventArgs e) label1.Text = ip.Pre(); private void button4_Click(object sender, EventArgs e) label1.Text = ip.Stop(); private void button5_Click(object sender, EventArgs e) label1.Text = ip.Play(); private void button6_Click(object sender, EventArgs e) label1.Text = ip.Pause(); private void button7_Click(object sender, EventArgs e) label1.Text = ip.Next(); 运行结果:自我评价:达到了实验预计的效果 总结你在完成任务1-3的过程中遇到的问题及解决的方法:完成的比较顺利,基本没有遇到问题总结本次实验涉及到的基本原理:C#窗体设计,面向对象的高级程序设计本次实验小结:通过这次试验,对抽象类和派生类有了更深的理解要求:1. 报告格式和内容要求: a. 内容和格式整齐。大标题采用黑体四号字加粗,小标题采用小四号字加粗。正文采用五号宋体,单倍行距。 b. 贴图时请剪裁到适当大小,要保证打印时可以看清,但也不要太大以免“越界”。 c. 不要在报告中写与实验无关的话,内容要有条理、完整、并能突出重点,要将遇到的主要问题说明。2. 文件格式要求: a. 将实验成果放入一个文件夹中,文件夹的内容包括:本实验报告、项目。 b. 文件夹以“学号姓名_S1”为文件名。其中,S1表示这是实验1的报告,S大写,以后的实验报告以类似方法编号顺延,注意:你的学号放在姓名前。 c. 注意:所有文件保存后关闭,然后再打包成RAR文件,以免提交的内容丢失或打不开。文档来源网络及个人整理,勿用作商业用途3. 提交方式和时间:一周内完成。可以将文件包发到我邮箱zcwang。4. 主动查阅资料,坚持自己亲手完成实验,弄清每个步骤和相关原理。-