I have always been vague about the concept of encapsulation. What is encapsulation?
In fact, encapsulation is everywhere when we write code.
First, classes and objects are the embodiment of encapsulation. Encapsulate the common features of some column objects as a class, and encapsulate the features of a specific thing as an object.
In addition, the following are encapsulation:
1. Attribute encapsulation of fields
2. encapsulate multiple parameters of a method into an object
3. Put a bunch of code with a specific function into a method,
4. encapsulate some common types or methods into a set of programs.
What are the advantages of encapsulation?
I have summarized the following points:
1. Code can be reused. Write frequently used functions into a method.
2. The caller does not have to worry about the specific internal implementation. Through the released interface, the caller can simply implement the call (and also hide the technical details ).
3. Security verification can be performed. If you do not want to assign values directly to a value of an object, you can encapsulate it and perform security verification internally.
OK. This is the encapsulation first.