zoukankan      html  css  js  c++  java
  • 用C# 设计一个 Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮(小学生、中学生、大学生)将分别创建不同的学生对象,并输出当前学生总人数,该学生的姓名、学生类型和平均成绩。

    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}:
    ", total() / 2);
                else if (type == "中学生")
                    result += string.Format(",平均成绩:{0:N2}:
    ", total() / 3);
                else
                    result += string.Format(",总学分为{0:N2}:
    ", 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.运行结果如下:

    依次输入不同年级学生的基本信息和各科成绩或者学分

     最终的三条记录展示如下:

    我是小关,关注我,带你从初级入门编程
    希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
    版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    大家写文都不容易,请尊重劳动成果~
    交流加Q:1909561302
    CSDN地址https://blog.csdn.net/Mumaren6/

  • 相关阅读:
    Building a Space Station POJ
    Networking POJ
    POJ 1251 Jungle Roads
    CodeForces
    CodeForces
    kuangbin专题 专题一 简单搜索 POJ 1426 Find The Multiple
    The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool
    The Preliminary Contest for ICPC Asia Shenyang 2019 H. Texas hold'em Poker
    The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
    robotparser (File Formats) – Python 中文开发手册
  • 原文地址:https://www.cnblogs.com/guanguan-com/p/14241656.html
Copyright © 2011-2022 走看看