Inheritance is a simple relationship between objects, allowing the derived class to fully obtain the features of the base class, and the derived class can also access some work code inside the base class (via protected members ). There are other important relationships between objects.
This section briefly discusses the following relationships:
Inclusion relationship
One class contains another class. This is similar to an inheritance relationship, but the contained class can control access to the members of the contained class, and even perform other processing before using the members of the contained class.
Set relationship
A class is used as a container for multiple instances of another class. This is similar to an object array, but the set has other functions, including indexing, sorting, and resetting the size.
1. Inclusion relationship
You can use a member field to include an object instance. This member field can be a public field. In this case, the container object user can access its methods and attributes, just like the inheritance relationship, use the internal code of the category class of the derived class.
In addition, the contained member objects can be converted into private members. In this way, users cannot directly access their members, even if they are public members. However, you can use the members that contain the class to access these private members. That is to say, you can fully control the members of the contained class. If there are members, you can perform other processing on the members that contain the class before being included.
Example: // Udder breast
The Cow class contains a Udder class, which has a common method of Milk. The Cow object can call this method as required, as part of the SupplyMilk () method, but the user of the Cow object cannot see these details.
In UML, the contained classes can be represented by correlated lines. For simple include relationships, you can use a line with 1 to describe the one-to-one relationship (a Cow instance contains a Udder instance)
8.2.4 relationship between objects