设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生,中学生,大学生等派生类,当输入相关数据,单击不同的按钮(小学生,中学生,大学生)将分别创建不同的学生对象,并输出当前学生总人数,该学生的姓名,学生类型和平均成绩。如图所示,要求如下:
(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.Text;
using System.Windows.Forms;
namespace 学生
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label6.Text = "";
}
//小学生信息
private void button1_Click(object sender, EventArgs e)
{
try
{
Pupil pu = new 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
{
Middle pu = new 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
{
College pu = new 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;
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(",总学分为a{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 ;
}
}
}
运行结果如下: