注意:C#语言刚入门,代码质量不高!!!可以参考
1.设计一个 Windows 应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输出当前的学生总人数,该学生的姓名、学生类型和平均成绩。如图5-11所示,要求如下:
(1)每个学生都有姓名和年龄
(2)小学生有语文、数学成绩。
(3)中学生有语文、数学和英语成绩。
(4) 大学生有必修课学分总数和选修课学分总数,不包含单科成绩
(5) 学生类提供向外输出信息的方法。
(6) 学生类提供统计个人总成绩或总学分的方法
(7) 通过静态成员自动记录学生总人数。
(8)能通过构造函数完成各字段成员初始化
控件名称如下:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 实验5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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 = 0.0;
if (!textBox5.Text.Equals(""))
{
sub3 = Convert.ToDouble(textBox5.Text); ;
}
junoir J = new junoir(textBox1.Text, age, sub1, sub2,sub3);
label6.Text += J.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);
double sub3 = 0.0;
if (!textBox5.Text.Equals(""))
{
sub3 = Convert.ToDouble(textBox5.Text); ;
}
colleage c = new colleage(textBox1.Text, age, sub1, sub2, sub3);
label6.Text += c.getInfo();
}
}
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 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 junoir : Student
{
protected double chinese;
protected double math;
protected double english;
public junoir(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 colleage : Student
{
protected double chinese;
protected double math;
protected double english;
public colleage(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;
}
}
}
2.设计一个 Windows 应用程序,在该程序定义平面图形抽象类和其派生类圆、矩形和三角形。该程序实现的功能包括:输入相应图形的参数,如矩形的长和宽,单击相应的按钮,根据输入参数创建图形类并输出该图形的面积。程序运行结果如图 5-12 所示。
控件名称如下
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 实验5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Circle c=new Circle(Convert.ToDouble(textBox2.Text));
label7.Text = ""+c.Area();
}
private void button2_Click_1(object sender, EventArgs e)
{
JuXing j = new JuXing(Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox1.Text));
label7.Text = "" + j.Area();
}
private void button3_Click(object sender, EventArgs e)
{
SanJaoXing S = new SanJaoXing(Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox1.Text));
label7.Text = "" + S.Area();
}
}
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 JuXing : Figure
{
double chang;
double kuan;
public JuXing(double chang,double kuan)
{
this.chang = chang;
this.kuan = kuan;
}
public override double Area()
{
return chang * kuan ;
}
}
public class SanJaoXing : Figure
{
double di;
double gao;
public SanJaoXing(double di,double gao)
{
this.di = di;
this.gao = gao;
}
public override double Area()
{
return (di*gao)/2.0;
}
}
}
3.声明一个播放器接口 IPlayer,包含 5 个接口方法:播放、停止、暂停、上一首有首。设计一个 Windows 应用程序,在该程序中定义一个MP3 播放器类和一个 AVI 播放器类,以实现该接口,最后创建相应类的实例测试程序,图 5-13 所示为当单击 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.Threading.Tasks;
using System.Windows.Forms;
namespace 实验5
{
public partial class Form3 : Form
{
IPlayer ip;
MP3 m;
AVI a;
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
m = new MP3();
ip = (IPlayer)m;
}
private void button5_Click(object sender, EventArgs e)
{
label1.Text = ip.Play();
}
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 button6_Click(object sender, EventArgs e)
{
label1.Text = ip.Pause();
}
private void button7_Click(object sender, EventArgs e)
{
label1.Text = ip.Next();
}
}
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视频";
}
}
}