C#实验5类的编程含实验小结
一、实验目的:
1.理解面向对象的概念,掌握C#的定义类和创建对象的方法。
2.区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性。
3.掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。
4.理解构造函数和终结器的作用机制。
二、实验设备:
- PC一台;
- 软件:Windows OS,Visual Studio 2010等。
三、实验内容:(编写程序调试输出结果)
1.设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生,中学生,大学生等派生类,当输入相关数据,单击不同的按钮(小学生,中学生,大学生)将分别创建不同的学生对象,并输出当前的学生总人数,该学生的姓名,学生类型和平均成绩。
-
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 实验5._1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
//小学生
-
private void button1_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
实验5._1.Form1.Student.Pupil pu = new 实验5._1.Form1.Student.Pupil(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text));
-
-
label6.Text = pu.getInfo();
-
-
textBox1.Text = "";
-
-
textBox2.Text = "";
-
-
textBox3.Text = "";
-
-
textBox4.Text = "";
-
-
textBox5.Text = "";
-
-
}
-
catch
-
{
-
MessageBox.Show("请按要求输入!");
-
}
-
}
-
//中学生
-
private void button2_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
实验5._1.Form1.Student.Middle pu = new 实验5._1.Form1.Student.Middle(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text), Convert.ToDouble(textBox5.Text));
-
-
label6.Text = pu.getInfo();
-
-
textBox1.Text = "";
-
-
textBox2.Text = "";
-
-
textBox3.Text = "";
-
-
textBox4.Text = "";
-
-
textBox5.Text = "";
-
-
}
-
catch
-
{
-
MessageBox.Show("请按要求输入!");
-
}
-
}
-
//大学生
-
private void button3_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
实验5._1.Form1.Student.College pu = new 实验5._1.Form1.Student.College(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text));
-
-
label6.Text = pu.getInfo();
-
-
textBox1.Text = "";
-
-
textBox2.Text = "";
-
-
textBox3.Text = "";
-
-
textBox4.Text = "";
-
-
textBox5.Text = "";
-
}
-
catch
-
{
-
MessageBox.Show("请按要求输入!");
-
}
-
}
-
public abstract class Student
-
{
-
protected string name;
-
protected int age;
-
protected static int count;
-
-
public Student(string name, int age)
-
{
-
this.name = name;
-
this.age = age;
-
count ;
-
}
-
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}岁", count, 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 if (type == "大学生")
-
{
-
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 College : Student //派生大学生类
-
-
{
-
protected double obligatory;
-
-
protected double elective;
-
-
public College(string name, int age, double obligatory, double elective)
-
-
: base(name, age)
-
-
{
-
this.obligatory = obligatory;
-
-
this.elective = elective;
-
-
}
-
-
public override string type
-
-
{
-
get
-
-
{
-
return "大学生";
-
-
}
-
-
}
-
-
public override double total()
-
-
{
-
return obligatory elective ;
-
-
}
-
-
}
-
}
-
}
-
}
2.设计一个Windows应用程序,在该程序定义平面图形抽象类和其派生类圆,矩形和三角形。该程序实现的功能包括:输入相应图形的参数,如矩形的长和宽,单机相应的按钮,根据输入参数创建图形类并输出该图形的面积。
-
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 实验5._2
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
-
{
-
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Circle c = new Circle(Convert.ToDouble(textBox1.Text));
-
richTextBox1.Text = "圆的面积为:" c.Area();
-
}
-
-
private void button2_Click(object sender, EventArgs e)
-
{
-
Trangle t = new Trangle(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
-
richTextBox1.Text = "矩形的面积为:" t.Area();
-
-
}
-
-
private void button3_Click(object sender, EventArgs e)
-
{
-
S s = new S(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
-
richTextBox1.Text = "三角形的面积为:" s.Area();
-
-
}
-
public abstract class Figure
-
{
-
public abstract double Area();
-
}
-
public class Circle:Figure
-
{
-
double r;
-
public Circle(double r)
-
{
-
this.r = r;
-
}
-
public override double Area()
-
{
-
return r * r * 3.14;
-
}
-
}
-
public class Trangle:Figure
-
{
-
double h;
-
double w;
-
public Trangle(double h,double w)
-
{
-
this.h = h;
-
this.w = w;
-
}
-
public override double Area()
-
{
-
return h*w;
-
}
-
}
-
public class S:Figure
-
{
-
double h;
-
double w;
-
public S(double h,double w)
-
{
-
this.h = h;
-
this.w = w;
-
}
-
public override double Area()
-
{
-
return w * h/2;
-
}
-
}
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
}
-
}
3.声明一个播放器接口IPlayer,包含5个接口方法:播放,停止,暂停,上一首和下一首。
设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个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 实验5._3
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
IPlayer iplayer;
-
MP3 mp3;
-
AVI avi;
-
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视频!";
-
}
-
}
-
-
private void button2_Click(object sender, EventArgs e)
-
{
-
avi = new AVI(); //新建AVI对象
-
iplayer = (IPlayer)avi; //将avi转化为接口
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
mp3 = new MP3(); //新建Mp3对象
-
iplayer = (IPlayer)mp3; //将MP3转换为接口
-
}
-
-
private void button3_Click(object sender, EventArgs e)
-
{
-
if (mp3 is IPlayer || avi is IPlayer) //判断
-
{
-
richTextBox1.Text = "\n" iplayer.Pre();
-
}
-
else
-
{
-
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
-
}
-
-
}
-
-
private void button4_Click(object sender, EventArgs e)
-
{
-
if (mp3 is IPlayer || avi is IPlayer) //判断类型
-
{
-
richTextBox1.Text = "\n" iplayer.Stop();
-
}
-
else
-
{
-
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");//判断是否为空
-
}
-
-
}
-
-
private void button5_Click(object sender, EventArgs e)
-
{
-
if (mp3 is IPlayer || avi is IPlayer)
-
{
-
richTextBox1.Text = "\n" iplayer.Play();
-
}
-
else
-
{
-
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
-
}
-
-
}
-
-
private void button6_Click(object sender, EventArgs e)
-
{
-
if (mp3 is IPlayer || avi is IPlayer)
-
{
-
richTextBox1.Text = "\n" iplayer.Pause();
-
}
-
else
-
{
-
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
-
}
-
}
-
-
private void button7_Click(object sender, EventArgs e)
-
{
-
if (mp3 is IPlayer || avi is IPlayer)
-
{
-
richTextBox1.Text = "\n" iplayer.Next();
-
}
-
else
-
{
-
MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
-
}
-
-
}
-
}
-
}
四、实验小结:
通过此次实验,理解面向对象的概念,掌握C#的定义类和创建对象的方法。.区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性。掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。理解构造函数和终结器的作用机制。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikigf
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24