--- 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