Dark Horse programmer 01-encapsulation Concept

Source: Internet
Author: User

--- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you! ---

1. encapsulation Concept

Hide implementation details, privatize attributes, and access private attributes through public methods. This process encapsulates objects.

In the actual programming process, we will encounter such a situation: When we directly call the member variables of an object to assign values to it, dirty data (meaningless data) will be generated ); for example, if we assign a person's age _ age to-20, the compiler will print it out, and the person's age cannot be negative. In this case, we are required to transform the implementation of methods within the class to filter out dirty data.

Solution: (1) prevent member variables from being directly accessed outside the class (2) filter the value of member variables within the method.

2. Advantages of encapsulation

(1) data is filtered to ensure data correctness.

(2) added code reusability

(3) privatize attributes to ensure the security of attributes

Dark Horse programmer 01-encapsulation Concept

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.