data encapsulation

  Encapsulation is to enclose procedures and data, and access to data can only be done through defined interfaces. Object-oriented computing begins with the fundamental concept that the real world can be represented as a series of fully autonomous, encapsulated objects that access other objects through a protected interface. Encapsulation is an information hiding technology, which is implemented in java through the keyword private. What is encapsulation? Encapsulation brings together all the components of an object. Encapsulation defines how a program references the object's data. Encapsulation actually uses methods to hide the class's data . It controls the extent to which users can modify the class and access the data.

  Why do you need encapsulation?

Through encapsulation, data access restriction to attributes can be realized, and the maintainability of the program can be increased at the same time.

Since the value acquisition method and the assignment method hide the changes of the implementation, it will not affect the class that reads or modifies the attribute, avoiding large-scale modification and enhancing the maintainability of the program.


Encapsulation steps: 1. Attribute privatization Modify the visibility of the attribute to limit access to the attribute (generally limited to private),

                   2. Provide public methods   to create a pair of assignment methods and value access methods for each attribute, which are used to access these attributes, commonly used 'get' and 'set'

                   3. In the assignment and value methods, add access restrictions to attributes.


The encapsulation provides 4 access control levels, and the orientation modifiers are:

1. public, the highest model level, the public members of the class can be accessed by all members of the class.

2.protected, protected members of a class are intelligently accessed by members of the class and members of its subclasses. It can also be accessed by members of other classes in the same package.

3.private Private, not open to the public, the private members of a class can only be accessed by members of the class, and the access level is the lowest.

4. By default, the members of the class have no modifiers, also known as package modifiers, only the members of the class itself and the members of the class under the current package can be accessed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856195&siteId=291194637