当前位置:首页 > C#程序设计实验报告实验指导书-邵佳楠
金陵科技学院实验报告
4、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。
程序要求:
(1)每个学生都有的字段为姓名、年龄。
(2)小学生的字段还有语文,数学,用来表示这两科的成绩。 (3)中学生在此基础上增加英语成绩。 (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 WindowsFormsApplication5 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
int age = Convert.ToInt32(txtAge.Text);
double sub1 = Convert.ToDouble(txtSub1.Text); double sub2 = Convert.ToDouble(txtSub2.Text); Pupil p = new Pupil(txtName.Text, age, sub1, sub2); lblShow.Text += p.getInfo(); }
private void button2_Click(object sender, EventArgs e) {
int age = Convert.ToInt32(txtAge.Text);
double sub1 = Convert.ToDouble(txtSub1.Text); double sub2 = Convert.ToDouble(txtSub2.Text); double sub3 = Convert.ToDouble(txtSub3.Text);
Middle p = new Middle(txtName.Text, age, sub1, sub2, sub3); lblShow.Text += p.getInfo();
15
金陵科技学院实验报告
}
private void button3_Click(object sender, EventArgs e) {
int age = Convert.ToInt32(txtAge.Text);
double sub1 = Convert.ToDouble(txtSub1.Text); double sub2 = Convert.ToDouble(txtSub2.Text); double sub3 = Convert.ToDouble(txtSub3.Text);
College p = new College(txtName.Text, age, sub1, sub2, sub3); lblShow.Text += p.getInfo(); } } }
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication5 {
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 == \小学生2\
16
金陵科技学院实验报告
result += string.Format(\平均成绩为{0:N2}:\\n\ else if (type == \中学生\
result += string.Format(\平均成绩为{0:N2}:\\n\ else
result += string.Format(\总学分为{0:N2}:\\n\ return result; } } }
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication5 {
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; } } }
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
17
金陵科技学院实验报告
namespace WindowsFormsApplication5 {
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; } } }
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication5 {
public class College : Student {
protected double chinese; protected double math; protected double english;
public College(string name, int age, double chinese, double math, double english) : base(name, age) {
this.chinese = chinese; this.math = math;
18
共分享92篇相关文档