分别利用List和ArrayList做一个小型的学生管理系统,实现添加学生和查询学生等功能?


class Program

    {
        static void Main(string[] args)
        {
            //创建学生实例
            Student student1 = new Student("小明", "研7", "unity3D", "001");
            Student student2 = new Student("小红", "研7", "unity3D", "002");
            Student student3 = new Student("小青", "研7", "unity3D", "003");
            Student student4 = new Student("小白", "研7", "unity3D", "004");
            Student student5 = new Student("小黑", "研7", "unity3D", "005");
            
            //集合法添加
            StudentList<Student> studentList = new StudentList<Student>();
            studentList.AddStudent(student1);
            studentList.AddStudent(student2);
            studentList.AddStudent(student3);
            studentList.AddStudent(student4);
            studentList.AddStudent(student5);
            studentList.FindStudent("小白");


            //动态数组法添加
            StudentArry.AddStudent(student1);
            StudentArry.AddStudent(student2);
            StudentArry.AddStudent(student3);
            StudentArry.AddStudent(student4);
            StudentArry.AddStudent(student5);
            StudentArry.FindStudent("小红");
        }

    }


    class StudentList<T> where T : Student//泛型约束有利于列表元素间接调用
    {
        List<T> list;//可不用泛型,直接声明为student类型
        public StudentList()
        {
            list = new List<T>();
        }
        public void AddStudent(T student)
        {
            list.Add(student);
        }
        public void FindStudent(string name)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Name() == name)
                {
                    list[i].Notice();
                }
            }
        }

    }


    static class StudentArry//动态数组法的功能实现
    {
        static Student student;
        static ArrayList arry = new ArrayList();
        public static void AddStudent(Student student)
        {
            arry.Add(student);
        }
        public static void FindStudent(string name)
        {
            for (int i = 0; i < arry.Count; i++)
            {
                student = (Student)arry[i];//用变量接收object(拆箱)
                if (student.Name() == name)
                {
                    student.Notice();
                }
            }
        }

    }


    class Student//学生类的声明、定义和初始化
    {
        string name;
        string grade;
        string lesson;
        string studentID;
        public string Name()//传统的读写封装方法
        {
            return name;
        }
        public string NAME//常用的读写封装属性
        {
            get { return name; }
            set { name = value;}
        }
        public Student(string name, string grade, string lesson, string studentID)
        {
            this.name = name;
            this.grade = grade;
            this.lesson = lesson;
            this.studentID = studentID;
        }
        public void Notice()
        {
            Console.WriteLine("{0},就读于{1}班级,{2}专业;学号为:{3}", name, grade, lesson, studentID);
        }
    }

猜你喜欢

转载自blog.csdn.net/wlong2017/article/details/79971723