zoukankan      html  css  js  c++  java
  • 3.11 封装集合

    【1】封装集合

    范例:

      1 #include <QString>
      2 #define MAX_STUDENT_COUNT 20
      3 
      4 class Student
      5 {
      6 public:
      7     Student(int nId, QString name = QString(), QString department = QString("jisuanji"))
      8         : m_nID(nId)
      9         , m_name(name)
     10         , m_department(department)
     11     {}
     12 
     13     int getId() const
     14     {
     15         return m_nID;
     16     }
     17     void setId(int nId)
     18     {
     19         m_nID = nId;
     20     }
     21     QString getName() const
     22     {
     23         return m_name;
     24     }
     25     void setName(QString name)
     26     {
     27         m_name = name;
     28     }
     29     QString getDepartment() const
     30     {
     31         return m_department;
     32     }
     33     void setDepartment(QString department)
     34     {
     35         m_department = department;
     36     }
     37 
     38     bool operator ==(const Student& student) const
     39     {
     40         return (student.getId() == m_nID &&
     41                 student.getName() == m_name &&
     42                 student.getDepartment() == m_department);
     43     }
     44 
     45 private:
     46     int m_nID;
     47     QString m_name;
     48     QString m_department;
     49 };
     50 
     51 class School
     52 {
     53 public:
     54     School()
     55     {
     56         for (int i = 1; i < MAX_STUDENT_COUNT + 1; ++i)
     57         {
     58             m_students.append(*(new Student(i)));
     59         }
     60     }
     61 
     62     // 获取到的仅仅只是一个副本
     63     const QList<Student> getStudents()
     64     {
     65         return m_students;
     66     }
     67 
     68     void addStudent(Student student)
     69     {
     70         m_students.append(student);
     71     }
     72 
     73     void removeStudent(int nId)
     74     {
     75         QList<Student>::const_iterator cIter = m_students.cbegin();
     76         while (cIter != m_students.cend())
     77         {
     78             if ((*cIter).getId() == nId)
     79             {
     80                 m_students.removeOne(*cIter);
     81                 return;
     82             }
     83             ++cIter;
     84         }
     85     }
     86 
     87 private:
     88     QList<Student> m_students; // 集合
     89 };
     90 
     91 /*
     92  * 有个函数返回一个集合。让这个函数返回该集合的一个只读副本,并在这个类中提供添加/移除集合元素的函数。
     93  */
     94 
     95 void main()
     96 {
     97     School school;
     98     QList<Student> studentList = school.getStudents();
     99     Student student(22, "liu", "math");
    100     school.addStudent(student);
    101     school.removeStudent(12);
    102 }

    【2】总结

    有个函数返回一个集合。让这个函数返回该集合的一个只读副本,并在这个类中提供添加/移除集合元素的函数。

    Good Good Study, Day Day Up.

    顺序 选择 循环 总结

  • 相关阅读:
    算法之递归
    初读 c# IL中间语言
    sql语句转为Model
    WPF-悬浮窗(类似于360)
    call,apply
    作用域题目
    css BFC
    数组扁平化 flatten
    常见的异步题
    setTimeout、Promise、Async/Await 的区别
  • 原文地址:https://www.cnblogs.com/Braveliu/p/7358301.html
Copyright © 2011-2022 走看看