Encapsulation (encapsulation) is to hide part of the data from the rest of the application, and
Ability to restrict the rest of the program code from accessing this data
Encapsulation is to hide the implementation of the class so that it is easy to apply and change. Encapsulates a user who makes a class available to it in a black box.
Encapsulation follows the open and closed principle in two areas:
Opening up to extensions means that existing code can be extended to accommodate new situations when there are new requirements or changes.
Enclosing a modification means that once the class is designed, it can do its work independently, rather than making any modifications to the class.
Open-Close principle (OCP)
1, software entities (classes, modules, functions, and so on) should be extensible, but can not be modified. that is, open for expansion, change-off
2, the opening and closing principle means that, when you design, time to consider, try to make this class is good enough, write well do not go to modify, if new requirements come, we add some classes on it, the original code can not move.
3, regardless of the module is how closed, there will be some can not be closed to the change. Since it is impossible to be completely closed, the designer must make a choice for the modules he has designed to be closed, and he must first guess what kind of changes are most likely to occur and then construct abstractions to isolate those changes.
4. In the face of demand, changes to the program are made by adding new code, rather than changing the existing code.
5. For the extensible part of the separation, the formation of an interface, the most likely to expand in the system is the business function of the increase or change for the business process should be as an extensible part of the implementation, when there is a new function increase, do not need to re-comb the formed business interface,
How to achieve coupling degree and flexibility both the dual mechanism?
The answer is abstraction. The business function is abstracted as an interface, and when the salesman relies on the fixed abstraction, it is closed to the modification, and the new extension implementation is derived from the abstract body through the inheritance and polymorphic mechanism, which is the opening to the extension.
The meaning of encapsulation is to protect the information in the program code from being used incorrectly.
Example: public class airplane{
public int speed; Note: The speed variable is public, not private, and now all parts of the application have direct access to the speed
Public airplane () {
}
public void Setspeed (int. speed) {
This.speed=speed;
}
public int getspeed () {
return speed;
}
}
Q: Does encapsulation completely match all of your variables to be private?
Encapsulation separates the data from the behavior of the application. Use member variables instead of letting other parts of the application
Manipulate your data directly by turning the data into private. If the data needs to be updated,
You can provide method handling, such as using GetSpeed () and Setspeed () in the airplane class.
Can control how each part is used by other parts of the application.
Object-Oriented Programming