在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.题目要求如下: 用C# 设计一个 Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输出当前学生总人数,该学生的姓名、学生类型和平均成绩。 如下图所示,要求如下: (1)每个学生都要姓名和年龄。
(2)小学生有语文、数学成绩。 (3)中学生有语文、数学和英语成绩 (4)大学生有必修课学分总数和选修课学分总数,不包含单科成绩。 (5)学生类提供向外输出信息的方法。 (6)学生类提供统计个人总成绩或总学分的方法。 (7)通过静态成员自动记录学生总人数。 (8)能通过构造函数完成各字段成员初始化。 2.来吧展示,代码如下:
using System; using System.Windows.Forms; namespace All_Student_4._1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { richTextBox1.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)); richTextBox1.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)); richTextBox1.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)); richTextBox1.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(",总学分为{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; } } }
3.运行结果如下: 依次输入不同年级学生的基本信息和各科成绩或者学分
最终的三条记录展示如下:
我是小关,关注我,带你从初级入门编程 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论