8.2.4 the relationship between objects
Inheritance is a simple relationship between objects that allows derived classes to get the attributes of the base class intact, while derived classes can access some work code inside the base class (through protected members).
There are other important relationships between objects.
This section briefly discusses the following relationships:
Include relationship: One class contains another class. This is similar to the world relationship, but the containing class can control access to the members of the contained class, even 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 array of objects, but the collection has other features, including indexing, sorting, and resizing.
1. Include Relationships
You can implement a include (containment) relationship by using a member field that contains an object instance. This member field can be a public field, and as with inheritance, the user of the container object can access its methods and properties, but it cannot access the internal code of the class through a derived class, as it does for an inheritance relationship.
In addition, you can make the contained member object private, and if you do so, the user cannot directly access any of the members, even if they are public and cannot be accessed. However, you can use members of the containing class before you ask these private members. That is, you have full control over what members of the contained classes are, and if you have members, you can also perform additional processing on the members of the containing class before accessing the members of the contained class.
For example, the Cow class contains a udder class that has a public method milk (). The Cow object can call this method as required, as part of its supplymilk () method, but the user of the Cow object does not see these details.
In UML, a contained class can be represented by an associated line. For a simple containment relationship, you can use a line with 1 to illustrate one-to-one relationships (a cow instance contains a udder instance). For clarity, the included Udder class instance can also be represented as a private field of the cow class, as shown in 8-10.
2. Set Relationship
The 5th chapter discusses how to use arrays to store multiple homogeneous variables. This also applies to objects (the variable type used earlier is actually an object). For example:
New animal[5];
A collection is basically an array, and the collection is implemented as a class in the same way as other objects. They are usually named in the plural form of the stored object name, such as class animals, which contains a collection of animal objects.
The main difference between arrays and collections is that collections typically implement additional functionality, such as the Add () and remove () methods to add and remove items from a collection. The collection usually has an item property, which returns the object based on the object's index. Moreover, this property allows for more complex access methods. For example, you can design a animals that allows the animal object to be accessed according to its name.
In UML, this is represented in Figure 8-11.
The members are omitted here because the relationship is described here. The number at the end of the connector indicates that a Animals object can contain 0 or more animal objects. The 11th chapter will discuss the collection in detail.
(original) C # learning note 08--Introduction to Object-oriented programming the relationship between 02--oop technology 04--objects