简体   繁体   English

对象之间的相互作用

[英]Interaction between objects

If I have a class Music like this 如果我有这样的音乐课

class Music{
  private:
    string song[20];
    string album[20];
    string artistName[20];
    string genre[20];
    static int i;
    int j;
    int duration[20];
  public:
    Music(string musicName,string artist,string genre,string albumname,int duration);
    void  addMusic(string musicName,string artist,string genre,string albumname,int duration);
    bool browseMusic(string musicName);
    void getParticularSongDetail(string musicName);
    void totalNumberOfSongs();

};

and a person class like this 还有一个像这样的人

class Person{

  public:
    Person(string,string,string);
    string getFirstName();
    string getMiddleName();
    string getLastName();
    int getId();
  private:
    //some variables
}

how can I add 20 songs to Music class that belongs to a particular person? 如何将20首属于特定人物的歌曲添加到音乐课?

The answer really depends on how you wish to design your program. 答案实际上取决于您希望如何设计程序。

The following are some possible solutions for you: 以下是一些可能的解决方案:

  1. The Person could have an array of Music objects. 该人可能具有一组Music对象。

  2. The Music class could have a keep track of the Peron's id it belongs to. 音乐课可以跟踪所属的Peron的ID。

  3. The Music classes may be something you won't want to repeat between users, so you might make a 3rd class which helps make the associations. 音乐类可能是您不想在用户之间重复的内容,因此您可以制作第三个类来帮助建立关联。 (eg Multiple people might own the same Tool album CD/mp3, so another class could help you make the associations, or you might have an array in your Music class to keep track of multiple Persons... Normally a "list" would be a better datatype than an array, however from your code examples I stuck to arrays to keep the response simple). (例如,多个人可能拥有相同的工具专辑CD / mp3,因此另一个类可以帮助您建立关联,或者您的音乐类中可以有一个数组来跟踪多个人。通常,“列表”是比数组更好的数据类型,但是从您的代码示例中,我坚持使用数组以使响应保持简单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM