UML 类图之间关系
1.依赖Dependency:
//Dependency class Boat{ Boat(){} }; class Man{ Man(){} void river_crossing(Boat boat){} };
其中 Boat 作为Man river_crossing(过河)的参数
2.关联Association:
//Association class Grade{ Grade(){} }; class Student{ Student(); private: Grade grade; };
其中 grade 作为Student的一个私有变量
3聚合Aggregation:
//Aggreation #define N 10 class Child{ Child(); }; class Family{ Family(); private: Child child[N]; };
这里强调的是has-a
4组合Composition:
//Composition class Room{ public: Room(){} }; class House{ public: House(){ room = new Room; } private: Room *room; };
这里强调Room随House生命周期结束而结束
5实现Relization:
//Relization class Gun{ public: virtual ~Gun()=0; virtual void shoot()=0; }; class AK47 :public Gun{ virtual ~AK47() override; void shoot() override{ std::cout<<"AK47 shooting"<<std::endl; } };
6继承 Generalization:
class People{ public: People(); }; class Student :public People{ public: Student(); };
抽象类与接口的区别:
抽象类:类中存在纯虚函数,即不能生成对象的类
接口:接口也是特殊的类。但还要满足:
(1)类中没有定义任何成员变量
(2)类中所有成员函数皆是公有且都是虚函数
强弱关系: 泛化 = 实现 > 组合 > 聚合 > 关联 > 依赖