Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
To summarize the previous content, the object refers to the type of a thing, class, which refers to an image. objects can have states and actions, that is, data members and methods.
Until now, data members and methods have been open to both internal and external. Inside the object, we use this to invoke the object's data members and methods. Objects can be used outside the object, such as when we call an object in another class . Data members and objects. Method () to invoke the object's data members and methods.
We are going to encapsulate (encapsulation) The members of the object (members include data members and methods), thus allowing only the members of the part to be called from outside. With encapsulation, we can improve the ease of use and security of objects.
Encapsulation and Interface
Encapsulation (encapsulation) is a common term for computers that retains a limited number of external interfaces (interface), hiding specific implementation details. For example, in the Linux architecture, you can see that the Linux operating system encapsulates the specifics of the underlying hardware, leaving only the system calls to this set of interfaces. The user is outside the package, only through the interface, to do the required operations.
Encapsulation is common in life. For example, here is a charging torch:
A user can guess the operation of the flashlight even if he doesn't read the instructions: switch and charge. The torch is a plastic shell that hides the internal details that the user does not need to touch, leaving only two connectors, switches and electrical plugs. With these two interfaces, the user is sufficient to use the functionality that the product wants to implement in the design. If all the details are exposed to the user at the same time, the user will feel overwhelmed with the product (e.g. a remote control without shell below). As a result, encapsulation 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 and so on. While this allows the user to operate the product more freely, such as discharging the battery directly, removing an LED lamp, etc. However, users often have to bear the risk of greater damage to the product. As a result, the package improves product safety.
A Java software PRODUCT is the same as a daily product. An object can have many members (data members and methods) inside it. There are some data members and methods that are only used internally. At this point, we would like to have a mechanism to "shell" the object, thus encapsulating the object. This makes it easier for users to learn and use external interfaces without having to touch internal members.
encapsulation of Object Members
Java controls the external visibility of members of an object by three keywords (visibility): Public , private, protected.
- Public: The member is visible externally, that is, the member is part of the interface
- Private: The member is not visible externally and can only be used for internal use and cannot be accessed externally.
(protected involves the concept of inheritance, put it in the future.)
Let's first encapsulate the previously defined human class:
PublicClasstest{PublicStaticvoidMain (string[] args) {Human Aperson =NewHuman (160); System.out.println (Aperson.getheight ());
Aperson.growheight (170);
System.out.println (Aperson.getheight ());
Aperson.repeatbreath (100); }}Classhuman{/*** Constructor*/Public Human (Inth) {This.height =H System.out.println ("I ' m Born"); }/*** Accessor*/PublicIntGetHeight () {ReturnThis. Height; }/*** Mutator*/Publicvoid Growheight (Inth) {This.height =This.height +H }/*** Encapsulated, for internal use*/Privatevoid breath () {System.out.println ("hu...hu ..." ); } /** call Breath () */ Span style= "color: #0000ff;" >public void Repeatbreath (int Rep) {int I; for (i = 0; i < rep; I++this.breath ();}} private int height; // encapsulated, for internal use}
Internal methods are not affected by encapsulation. Human's internal method can call any member, even the height and breath () set to private
External methods can only invoke public members. When we are outside of human, such as test, we can only call members that are defined as public in human, and cannot invoke a member that is defined as private.
By encapsulation, the human class retains only the following methods as interfaces:
- GetHeight ()
- Growheight ()
- Repbreath ()
We can represent the human class and its interfaces as a form:
"A remote control with a shell"
If we forcibly call height from main:
System.out.println (Aperson.height);
There will be the following error message:
Test.java:6: Height has private access in Human
System.out.println (Aperson.height);
^
1 Error
Beep, you got a shock! A member that is described as private and cannot be called externally.
In the general specification of Java, data members that express state (such as height) are set to private. Modifications to the data members are made through the methods provided by the interface (such as GetHeight () and Growheight ()). This specification plays a role in protecting data. The user cannot modify the data directly, and the data must be read and written by the appropriate method. The designer of the class can include the usage specification of the data in the interface method.
Encapsulation of Classes
In a. java file, there is only one class with the public keyword, such as the test class above. So, from any other class, we can call that class directly. The human class has no keywords. Earlier, the members of our object also had no keywords. This non-keyword scenario also represents a visibility that I'll dive into in the package.
Practice encapsulating a torch class to represent a flashlight. The interface is switched on and charged. The internal members have power.
Summary
Encapsulation, interface
Private, public
Java basic 04 Package and interface