设计模式之 has-a

设计模式之 has-a

在 C++中,一个类包含另一个类的对象称为组合(Composition)。这是一种常见的设计模式,用于表示一个类是由另一个类的对象组成的。这种关系通常表示一种"拥有"(“has-a”)的关系。

  • 普通变量访问成员变量或者成员函数,使用 “ . ” 运算符
  • 指针变量访问成员变量或者成员函数,使用“ -> ”运算符,像C语言的结构体用法
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

class Wheel
{
public:
    string brand;
    int year;

    void wheelPrintInfo();
};

void Wheel::wheelPrintInfo()
{
    cout << "我的轮胎品牌是:" << brand << endl;
    cout << "我的轮胎日期是:" << year << endl;
}

//在 C++中,一个类包含另一个类的对象称为组合(Composition)。
class Car{       //汽车“类”
public:
    // 成员数据
    string color;  //颜色
    string brand;  //品牌
    string type;   //车型
    int year;     //年限

    Wheel wl;
    Wheel *pwl;


    // 成员数据
    void (*printCarInfo)(string color, string brand, string type, int year); //函数指针,指向车介绍函数
    void (*carRun)(string type);       //函数指针,指向车运行的函数
    void (*carStop)(string type);      //函数指针,执行车停止的函数

    // 成员函数
    void realPrintCarInfo();
};

void Car::realPrintCarInfo()
{
    cout << "成员函数打印" << endl;
    string str = "车的品牌是:" + brand
               + ",型号是: " + type
               + ",颜色是:" + color
               + ", 上市年限是:" + std::to_string(year);
    cout << str << endl;
}

void bwmThreePrintCarInfo(string color, string brand, string type, int year)
{
    string str = "车的品牌是:" + brand
               + ",型号是: " + type
               + ",颜色是:" + color
               + ", 上市年限是:" + std::to_string(year);
    cout << str << endl;
}

void A6ThreePrintCarInfo(string color, string brand, string type, int year)
{
    string str = "车的品牌是:" + brand
               + ",型号是: " + type
               + ",颜色是:" + color
               + ", 上市年限是:" + std::to_string(year);
    cout << str << endl;
}

int main()
{
    Car BWMthree;
    BWMthree.color = "白色";
    BWMthree.brand = "宝马";
    BWMthree.type = "3系";
    BWMthree.year = 2023;
//    BWMthree.wl.brand = "米其林";
//    BWMthree.wl.year = 2023;
//    BWMthree.wl.wheelPrintInfo();

    BWMthree.pwl = new Wheel();
    BWMthree.pwl->brand = "米其林";
    BWMthree.pwl->year = 2023;
    BWMthree.pwl->wheelPrintInfo();

    BWMthree.printCarInfo = bwmThreePrintCarInfo;
    BWMthree.printCarInfo(BWMthree.color, BWMthree.brand, BWMthree.type, BWMthree.year);
    BWMthree.realPrintCarInfo();

    Car *AodiA6 = new Car;
    // AodiA6 = (struct Car *)malloc(sizeof(struct Car));
    AodiA6->color = "黑色";
    AodiA6->brand = "奥迪";
    AodiA6->type = "A6";
    AodiA6->year = 2008;

    AodiA6->printCarInfo = A6ThreePrintCarInfo;
    AodiA6->printCarInfo(AodiA6->color, AodiA6->brand, AodiA6->type, AodiA6->year);
    AodiA6->realPrintCarInfo();
//    AodiA6->pwl->brand = "马牌";
//    AodiA6->pwl->year = 2023;
//    AodiA6->wl.wheelPrintInfo();

    AodiA6->pwl = new Wheel;
    AodiA6->pwl->brand = "马牌";
    AodiA6->pwl->year = 2023;
    AodiA6->pwl->wheelPrintInfo();

    return 0;
}