The most common relationships between classes and classes are mainly three: dependencies (uses-a), aggregations (has-a), and Inheritance (IS-A).
The following is an example of an online bookstore order system that details the concepts of these three relationships.
The main functions of the online bookstore Order system are: Registered users can login to the online bookstore to buy books, fill out the order online, and pay the purchase book. When the bookstore confirms that the book has been received, mail the book at the address left by the user. You can create several classes in this system, books (book), accounts (account), orders (order), addresses (address), and so on, as shown in the following illustration:
Dependence (USES-A)
Dependencies are the most common relationships in a class, such as order classes (orders) that require access to user account classes (account), so it is necessary to reference the accounts class in the order class, i.e. the order class relies on the account class, but the book class does not need to rely on account classes.
If you modify the account class, it will affect the order class. The essence of dependency is that methods in a class can manipulate instances of another class. In practical programming, it is recommended to minimize the number of interdependent classes. As shown in the following illustration:
Aggregation (HAS-A)
Because orders need to indicate what books to order, this involves books, that is, the book category. The difference between aggregation and dependency is that the order class can not have all the account class objects, but must have all the library objects, because the object of the book class is the main purpose of the order, as shown in the following illustration:
Inherited
Inheritance is a class that can call all data members of another class, and it does not need to be redefined in the current class, and this chapter is clear.