Contents
Abstract:
This paper mainly provides a basic introduction to the relationships between classes, including inheritance, composition and dependency, illustrates them with real-world examples, and meanwhile explains the characteristics and application scenarios of different relationships.
Source Link:
Previous Recommendations:
You who are learning embedded systems still don't know Object Oriented? ?!
More exciting content can be found here:
Speed Up Your Python: A Quick Guide to Python Parallel Computing
Understand the Debugging Principle of CM3 Microcontroller in One Article
The "Secret Martial Arts Manual" for Computer Science Competitions
Access to Documents and Code
You can access the following link to download the document:
https://github.com/leezisheng/Doc

This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the lower computer as application examples, and uses the Sourcetrail code software to visually read the code to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows: https://github.com/leezisheng/Python-OOP-Demo
Body Text
Simply put, there are three types of relationships between classes: the is-a relationship, the has-a relationship and the use-a relationship:
- The is-a relationship is also known as inheritance or generalization; for example, the relationship between students and humans, and the relationship between mobile phones and electronic products, both fall under inheritance relationships. In Object Oriented programming, a class can inherit attributes and methods from another class.
- A has-a relationship is commonly referred to as an association; for example, the relationship between a department and its employees, or that between a car and its engine, both fall under association relationships. When an association describes a whole-part connection, we call this an aggregation relationship; if the whole further takes charge of the lifecycle of the part — meaning the whole and the part are inseparable, coming into existence and ceasing to exist together — then this is the strongest form of association, which we call a composition/composite relationship.
- The use-a relationship is commonly referred to as dependency. For example, a driver has a driving behavior (method) whose parameters involve the use of a car, then the relationship between the driver and the car is a dependency relationship.
For a concrete visualization, please refer to the UML class diagram below:

By leveraging these relationships between classes, we can perform certain operations based on existing classes, and also create new classes on the basis of existing classes; these are all important means to achieve code reuse. Reusing existing code not only reduces the workload of development, but also facilitates the management and maintenance of code, which is a technical method we all apply in our daily work.
