C#——3.设计一个窗体程序,要求如下:定义泛型的班级类对象,完成对学生的添加和信息的输出。

 

(1)构造一个学生基本类;

(2)分别构造小学生、中学生、大学生等派生类,要求具有不同的特征和行为

(3)定义一个泛型班级类,约束参数类型为学生类,该泛型班级类包括一个泛型集合,用于存放各种学生对象,并包含一个方法用于输出每个学生的相关信息。

(4)定义泛型的班级类对象,完成对学生的添加和信息的输出。

 

设计界面:

编写代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 泛型类高级应用
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "";
        }

        Pet<Student> myPet = new Pet<Student>();
        //添加小学生
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                myPet.Students.Add(new Pupil(textBox1.Text));
                label1.Text = string.Format("\n添加小学生:{0}成功", textBox1.Text);
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("请输入小学生的姓名!");
            }
        }
        //添加中学生
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                myPet.Students.Add(new Middle(textBox1.Text));
                label1.Text = string.Format("\n添加中学生:{0}成功", textBox1.Text);
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("请输入中学生的信息!");
            }
        }
        //上学
        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text ="全体学生信息如下:\n"+ myPet.goSchool();
        }
        //添加大学生
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                myPet.Students.Add(new College(textBox1.Text));
                label1.Text = string.Format("\n添加大学生:{0}成功", textBox1.Text);
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("请输入大学生的信息!");
            }
        }
    }

    //学生类——抽象类
    public abstract class Student
    {
        protected string name;
        public Student(string name)
        {
            this.name = name;
        }
        public abstract string Action();
    }
    //小学生类——派生类
    public class Pupil : Student
    {
        public Pupil(string name) : base(name) { }
        public override string Action()
        {
            return string .Format ("{0}:我是小学生,我在实验小学!",name);
        }
    }
    //中学生类——派生类
    public class Middle  : Student
    {
        public Middle(string name) : base(name) { }
        public override string Action()
        {
            return string.Format ("{0}:我是中学生,我在北京十一中学!",name);
        }
    }
    //大学生类——派生类
    public class College : Student
    {
        public College(string name) : base(name) { }
        public override string Action()
        {
            return string .Format ("{0}:我是大学生,我在清华大学!",name);
        }
    }
    //泛型类——与Student有关类
    public class Pet<T> where T : Student
    {
        private List<T> students = new List<T>();
        public List<T> Students  
        {
            get
            {
                return students;
            }
        }
        //行为类
        public string goSchool()
        {
            string msg = "";
            foreach (T x in students)
            {
                msg += "\n" + x.Action();
            }
            return msg;
        }
    }
}

 

运行结果:

 

 

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88918750