C#类的继承与多态性练习

设计一个windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生等派生类,当输入相关数据,单击不同的按钮将分别创建不同的学生对象。

核心代码出自《C#程序设计经典教程(第三版)》P142

namespace Test5_1
{
   //学生类
    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 int age  1.为什么age不行
        //{
        //    get { return age; }
        //}

        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};\r\n", total() / 2);  //{0:N2}是什么意思?
            else if (type == "中学生")
                result += string.Format(",平均成绩为{0:N2};\r\n", total() / 3);
            else
                result += string.Format(",平均成绩为{0:N2};\r\n", total());  //换行符 \r\n
            return result;
        }
    }
}

namespace Test5_1
{
    //小学生类
    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;
        }

    }
}
namespace Test5_1
{
    //中学生类
    public class MiddleSchoolStudent : Student
    {
        protected double chinese;
        protected double math;
        protected double english;
        public MiddleSchoolStudent(string name, int age, double chinese, double math, double english) : base(name, age)
        {
            this.chinese = chinese;
            this.age = age;
            this.english = english;
        }
        public override string type
        {
            get
            {
                return "中学生";

            }
        }
        public override double total()
        {
            return chinese + math + english;
        }
    }
}

namespace Test5_1
{
    //大学生类
    public class Undergraduate : Student
    {
        protected double CompulsoryCredits;  //必修学分
        protected double Electives; //选修学分

        public Undergraduate(string name, int age, double CompulsoryCredits, double Electives) : base(name, age)
        {
            this.CompulsoryCredits = CompulsoryCredits;
            this.Electives = Electives;
        }
        //public override float total()   //返回类型必须是double才能重写成员“Student.total()匹配”   所以即使是学分的类型不会是double 这里也仍用double型
        //{
        //    return CompulsoryCredits + Electives;
        //}
        public override double total()
        {
            return CompulsoryCredits + Electives;
        }
    }

}
namespace Test5_1
{

    public partial class Test5_1 : Form
    {
        public Test5_1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //需要添加容错处理功能,,如果用户输入的不是数字
            int age = int.Parse(txtage.Text);
            double chinese = double.Parse(txtchinese.Text);
            double math = double.Parse(txtmath.Text);
            //Pupil p = new Pupil(txtname.Text, age, chinese, math);// 2.未能找到类型或命名空间名"Pupil"????
            Pupil p = new Pupil(txtname.Text, age, chinese, math);
            lblShow.Text += p.getInfo();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int age = int.Parse(txtage.Text);
            double chinese = double.Parse(txtchinese.Text);
            double math = double.Parse(txtmath.Text);
            double english = double.Parse(txtenglish.Text);

            MiddleSchoolStudent p = new MiddleSchoolStudent(txtname.Text, age, chinese, math, english);
            lblShow.Text += p.getInfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int age = int.Parse(txtage.Text);
            double CompulsoryCredits = double.Parse(txtchinese.Text);
            double Electives = double.Parse(txtmath.Text);


            // Undergraduate p = new Undergraduate(txtname.Text, age, CompulsoryCredits, Electives);// 3.无法从double转化成float
            Undergraduate p = new Undergraduate(txtname.Text, age, CompulsoryCredits, Electives);
            lblShow.Text += p.getInfo();
        }

    }





}


实验5_1:

  1. 输出结果未能成功换行
    C#类的继承与多态性练习_第1张图片
    C#类的继承与多态性练习_第2张图片

原因:windows下“\n”不能实现换行,将其改为“\r\n”后可实现换行

https://zhidao.baidu.com/question/386412786.html
C#类的继承与多态性练习_第3张图片
C#类的继承与多态性练习_第4张图片
C#类的继承与多态性练习_第5张图片

  1. 在学生父类Student中 {0,N2}的意思
    C#类的继承与多态性练习_第6张图片
    课本P42页,关于Format的应用,{0,N2}表示输出带两位小数位的数字。

  2. Pupil p = new Pupil(txtname.Text, age, chinese, math);
    问题:未能找到类型或命名空间名"Pupil"????
    原因:没有在解决方案管理器中为每个类建立单独的一个类即分部类,把所有代码都写在了Program.cs中,这样的话调用子类Pupil需要写为:Student.Pupil p = new Student.Pupil(txtname.Text, age, chinese, math);

或者为每个类单独建立类(分部类):
C#类的继承与多态性练习_第7张图片

  1. 大学生类中,学分总分类型total()应该是整数int型,但是在设置类型时仍需与前面Student类中的 double total()保持一致,否则不匹配
    在这里插入图片描述

你可能感兴趣的:(C#类的继承与多态性练习)