官术网_书友最值得收藏!

Composition

When using composition, this is a strong HAS-A relationship, and this is when a class contains one or more instances of another class. Unlike aggregation, these instances are not created on their own but, instead, are created in the constructor of the class and then destroyed by its destructor. Put into layman's terms, they can't exist separately from the whole.

In this case, we have created some new properties for the Enemy class, adding in combat skills that it can use, as in the Pokémon series. In this case, for every one enemy, there are four skills that the enemy will be able to have:

class AttackSkill 
{
public:
void UseAttack(void);
private:
int damage;
float cooldown;
};

class Enemy
{
public:
void GetHealth(void) const;
void SetHealth(int);
private:
int currentHealth;
int maxHealth;
AttackSkill skill1;
AttackSkill skill2;
AttackSkill skill3;
AttackSkill skill4;
};

The line in the diagram looks similar to aggregation, aside from the fact that the diamond is filled in:

主站蜘蛛池模板: 民和| 苗栗市| 德兴市| 兴化市| 五台县| 甘德县| 赣榆县| 达孜县| 海阳市| 高雄县| 宁武县| 讷河市| 宜良县| 周宁县| 阿尔山市| 镇远县| 屏南县| 长汀县| 嘉义市| 会东县| 玉龙| 泰顺县| 香河县| 化隆| 松江区| 启东市| 西城区| 长寿区| 石嘴山市| 岚皋县| 普安县| 凤阳县| 富平县| 雅江县| 丹棱县| 河南省| 翼城县| 青神县| 棋牌| 宜章县| 历史|