Read summary 3

Source: Internet
Author: User

Encapsulation meaning

(1) fields are usually defined as private, and attributes are usually implemented as public, while methods are implemented as private internally and externally as public to ensureReliability read/write control of internal dataIt protects data security and reliability, and provides effective interaction with external interfaces. This is the basic mechanism for effectively encapsulating classes. For example:

Public String Password {
Get { Return Password ;}
Set {
If (Value. Length < 6 )
Password = Value;
}
}

(2) The general understanding is as mentioned above, but the specific operation depends on the actual design requirements. For example, sometimes the attribute is implemented as private, it is better to set the method to private. For example, in an ATM class, a counter may be required to record the number of updates or selections, which is unnecessary for the user, therefore, you only need to implement private in the ATM class. Similarly, some methods in the type are internal data operations, so they are also provided in private mode to achieve data security.

(3)From the perspective of memory and Data PersistenceEncapsulation attribute provides an effective means for data persistence. Because the attributes of an object are resident in the same way as those of an object during the memory period, as long as the object is not recycled, its attribute value will also exist and record the data of the last change to it.

(4) In object-oriented systems, encapsulation is far more important than field, attribute, and method control at the class design layer. The encapsulation we understand should be a software design method aimed at implementing UI separation. After a system or software is developed, we should consider the purpose of maintenance and upgrade, make sure that the external interface is absolutely stable. The interface stability is the foundation to ensure software compatibility, stability, and robustness regardless of how the internal functional implementation of the system changes. Therefore, the encapsulation in OO wisdom aims to ensure:

-Hiding system implementation details to ensure system security and reliability.

-Provides a stable external interface. Therefore, the relatively stable part of the system is often abstracted as an interface.

-Encapsulation ensuresCodeModularization improves software reuse and function separation.

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.