日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

建模的常用手段:组合与聚合

發布時間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 建模的常用手段:组合与聚合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 組合
      • 1.1 組合的基本概念
      • 1.2 組合的具體方式
      • 1.3 組合的具體實例
    • 2 聚合
      • 2.1 聚合的基本概念
      • 2.2 聚合的具體實例

1 組合

1.1 組合的基本概念

當兩個對象之間是整體與部分的關系時,它們之間就是組合的關系。

對于如下問題:
構建一個計算機類,一臺計算機,由CPU芯片,硬盤,內存等組成。CPU芯片也使用類來表示。

計算機類和CPU類就是組合關系,組合關系具有如下特點:

  • 被擁有的對象(芯片)的生命周期與其擁有者(計算機)的生命周期是一致的。
    • 計算機被創建時,芯片也隨之創建。
    • 計算機被銷毀時,芯片也隨之銷毀。
  • 擁有者需要對被擁有者負責,是一種比較強的關系,是整體與部分的關系。

1.2 組合的具體方式

具體組合方式:
1)被組合的對象直接使用成員對象。(常用)
2)使用指針表示被組合的對象,在構造函數中,創建被組合的對象;在析構函數中,釋放被組合的對象。

UML中的組合表示:

注意包含者使用實心菱形(UML畫圖工具:starUML)。

1.3 組合的具體實例

構建一個計算機類,一臺計算機,由CPU芯片,硬盤,內存等組成。CPU芯片也使用類來表示。

CPU類:

#pragma once#include <string>class CPU { public:CPU(const char *brand = "intel", const char *version="i5");~CPU(); private:std::string brand; //品牌std::string version; //型號 }; #include "CPU.h" #include <iostream>CPU::CPU(const char *brand, const char *version) {this->brand = brand;this->version = version;std::cout << __FUNCTION__ << std::endl; }CPU::~CPU() {std::cout << __FUNCTION__ << std::endl; }

Computer類:

#pragma once #include "CPU.h"class Computer { public:Computer(const char *cpuBrand, const char *cpuVersion, int hardDisk, int memory);~Computer(); private:CPU cpu; // Computer和CPU是“組合”關系int hardDisk; //硬盤, 單位:Gint memory; //內存, 單位:G }; #include "Computer.h" #include <iostream>Computer::Computer(const char *cpuBrand, const char *cpuVersion,int hardDisk, int memory):cpu(cpuBrand, cpuVersion) {this->hardDisk = hardDisk;this->memory = memory;std::cout << __FUNCTION__ << std::endl; }Computer::~Computer() {std::cout << __FUNCTION__ << std::endl; }

main.cpp:

#include <iostream> #include <Windows.h> #include <string> #include <string.h> #include "Computer.h"using namespace std;void test() {Computer a("intel", "i9", 1000, 8); }int main(void) {test();system("pause");return 0; }

構造與析構的順序如下:

Cpu::Cpu Computer::Computer Computer::~Computer Cpu::~Cpu

2 聚合

2.1 聚合的基本概念

聚合不是組成關系,被包含的對象,也可能被其他對象包含。
擁有者,不需要對被擁有的對象的生命周期負責。

UML中的組合表示:

2.2 聚合的具體實例

需求:給計算機配一臺音響。

Computer類:

#pragma once #include "CPU.h"class VoiceBox;class Computer { public:Computer(const char *cpuBrand, const char *cpuVersion, int hardDisk, int memory);~Computer();void addVoiceBox(VoiceBox *box); private:CPU cpu; // Computer和CPU是“組合”關系int hardDisk; //硬盤, 單位:Gint memory; //內存, 單位:GVoiceBox *box; //音箱 }; #include "Computer.h" #include <iostream> #include "VoiceBox.h"Computer::Computer(const char *cpuBrand, const char *cpuVersion,int hardDisk, int memory):cpu(cpuBrand, cpuVersion) {this->hardDisk = hardDisk;this->memory = memory;std::cout << __FUNCTION__ << std::endl; }void Computer::addVoiceBox(VoiceBox *box) {this->box = box; }Computer::~Computer() {std::cout << __FUNCTION__ << std::endl; }

main.cpp:

#include <iostream> #include <Windows.h> #include <string> #include <string.h> #include "Computer.h" #include "VoiceBox.h"using namespace std;void test(VoiceBox *box) {Computer a("intel", "i9", 1000, 8);a.addVoiceBox(box); }int main(void) {VoiceBox box;test(&box);system("pause");return 0; }

參考資料:

  • C/C++從入門到精通-高級程序員之路【奇牛學院】
  • 總結

    以上是生活随笔為你收集整理的建模的常用手段:组合与聚合的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。