8.2.4 relationship between objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.