设计一个Windows应用程序,在该程序中首先构造中学生、大学生等派生类,当输入相关数据,单击不同的按钮(“小学生”“中学生”“大学生”)将分别创建不同的学生对象,并输入当前的学生总人数,该学生的姓名学生类型和平均成绩.。要求如下。
(1)每个学生都有的字段为:姓名、年龄。
(2)小学生的字段还有语文、数学,用来表示这两科的成绩。
(3)中学生在(2)基础上多了英语成绩。
(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.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Pupil pupil;
Middle middle;
College college;
private void button1_Click(object sender, EventArgs e)
{
pupil = new Pupil(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text)); //新建小学生类
double average=pupil.Average(); //创建平均数,并计算
string show = "总人数:" + Pupil.number + ",姓名:" + Convert.ToString(txtName.Text)+",小学生,平均成绩为:"+average; //输出值
listBox1.Items.Add(show);
}
private void button2_Click(object sender, EventArgs e)
{
middle = new Middle(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text), Convert.ToDouble(txtEnglish.Text));//新建中学生类
double average = middle.Average(); //创建平均数,并计算
string show = "总人数:" + Pupil.number + ",姓名:" + Convert.ToString(txtName.Text) + ",中学生,平均成绩为:" + average; //输出值
listBox1.Items.Add(show);
}
private void button3_Click(object sender, EventArgs e)
{
college = new College(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text));//新建大学生类
double average = college.Average(); //创建平均数,并计算
string show = "总人数:" + Pupil.number + ",姓名:" + Convert.ToString(txtName.Text) + ",大学生,平均成绩为:" + average; //输出值
listBox1.Items.Add(show);
}
}
public abstract class Student //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 abstract double Average(); //计算平均值
}
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 double Average()
{
return (chinese + math) / 2; //计算平均值
}
}
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 double Average()
{
return (chinese + math+english) / 3;
}
}
public class College : Student //大学生类
{
protected double chinese;
protected double math;
public College(string name, int age, double chinese, double math) : base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override double Average()
{
return (chinese + math ) / 2;
}
}
}
运行结果