Understand the idea of encapsulation

Encapsulation is the first of the three characteristics of object-oriented. The three characteristics of
object-oriented: encapsulation, inheritance, polymorphism

The role of encapsulation

1. Protect the class
Encapsulation can protect the internal details of the class to the utmost extent, that is to say, try not to let the caller of the class know what attributes are inside the class.
In actual development, there may often be illegal input, so right , Property encapsulation can protect the legal content of the property.
The use of encapsulation
first needs to set the encapsulated property to privatization, add private in front, and set the getter/setter method of the property (usually the development tool will provide a shortcut method, here is an example of idea, press ait+insert will Jump out of the corresponding options, select getter/setter, and then select the properties that need to be encapsulated and click ok to complete)
2. Code reusability

3. Keep the project structure in a regular state, which is convenient for developers to understand and maintain.
Article 3 can also be seen as a manifestation of the code reusability of Article 2.

Package classification

1. Encapsulation based on process The method of encapsulating
scattered operations (methods)/steps into one method
2. Encapsulation based on features
Abstract the actual entity into a class and encapsulate it as a class
3. Encapsulate
the scattered steps into a function based on function encapsulation ( method)

Guess you like

Origin blog.csdn.net/m0_45311187/article/details/113059824