Encapsulation and interface of java basic course

Summarizing the previous content, object refers to something, and class refers to the type of image. Objects can have states and actions, that is, data members and methods.

So far, data members and methods are open to both internal and external. Inside the object, we use this to call the data members and methods of the object. For objects outside, for example, when we call objects in another class, we can use objects. Data members and objects Method () to call the data members and methods of the object.

We will encapsulate the members of the object (members include data members and methods), so that only the members of the part can be called from the outside. Using encapsulation, we can improve the ease of use and security of objects.

Encapsulation and interface

Encapsulation is a common term in computers, that is, to retain a limited external interface and hide the specific implementation details. For example, in the Linux architecture, you can see that the Linux operating system encapsulates the specific details of the underlying hardware and only retains the system call interface. The user is outside the package and can only perform the required operations through the interface.

Encapsulation is very common in life. For example, here is a rechargeable flashlight:

Even without reading the manual, a user can guess the operation of the flashlight: switching and charging. The flashlight uses a plastic shell to hide the internal details that users do not need to touch, leaving only two interfaces, switches and electrical plugs. Using these two interfaces, users can use the functions that the product wants to achieve in the design. If all the details are exposed to the user at the same time, the user will feel overwhelmed by the product (such as the remote control without shell below). Therefore, packaging improves the ease of use of the product.

If the product is not encapsulated, many details of the flashlight or remote control will be exposed to the user: battery, circuit, sealed rubber, etc. Although this allows users to operate the product more freely, such as discharging the battery directly, taking out an LED lamp and so on. However, users often have to bear a greater risk of damaging products. Therefore, packaging improves the safety of the product.

A java software product is the same as a daily product. There can be many members (data members and methods) inside an object. Some data members and methods are only used internally. At this time, we will want to have a mechanism to "shell" the object, so as to encapsulate the object. In this way, users can easily learn and use external interfaces without contacting internal members.

Encapsulation of object members

Java controls the external visibility of object members through three keywords: public, private and protected.

1. Public: the member is externally visible, that is, the member is part of the interface. 2 Private: the member is invisible from the outside and can only be used for internal use. It cannot be accessed from the outside. (protected involves the concept of inheritance, which will be explained later)

Let's first encapsulate the previously defined human class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>