learn to program and for a while, now tidy up something, save yourself later, may also be able to help other people.
Packaging:
Encapsulation refers to hiding the state information of an object inside an object, not allowing external programs to directly access the information inside the object, but through the methods provided by the class to operate and access the internal information.
Encapsulation is an object-oriented programming language for the simulation of the objective world, the objective of the world's properties are hidden inside the object, the outside world can not directly manipulate and modify.
The benefits of encapsulation are many:
1. Implementation details of hidden classes
2, let the user only through the predetermined method to access the data, so that the method can be added to the control logic, restricting unreasonable access to attributes.
3, the data can be checked, so as to help ensure the integrity of the object information.
4, easy to modify, improve the maintainability of the code.
Access control characters:
There are three access controls available in Java: Private, protected and public, plus an access control level without any access control, so Java provides four levels of access control. namely: private,default,protected and public. The meanings represent private, default, protected, and public access modifiers, respectively. In these four access levels, the default does not have a corresponding access control, and the system uses the access control when the class and members (Properties/methods) are not decorated with any modifiers. These access modifiers can both decorate properties in a class and modify methods in a class, while public and default can also decorate classes. In the same Java file, the public class has only one, and the file is named the class name. Java.
The Java access modifier is the ability to determine the access rights of a property or method in a class, in other words, the scope of the properties and methods.
Private, privileged access, and the most stringent access rights, can only be accessed in the class in which the permission is set, and this access permission is used to demonstrate the encapsulation idea.
Default access, which can be omitted, can be accessed not only in the class in which the permission is set, but also in classes or subclasses in the same package.
Protected, Protected Access (subclass access), if a member (property or method) uses this access control modifier, then the member can be accessed either by another class in a package or by a subclass in a different package, usually If you use protected to modify a method, you usually want its subclasses to override this method.
Public, publicly accessible, and the most permissive access, can be accessed not only by the same class or subclass, or by a class or subclass in the same package, or by a class or subclass in a different package.
To understand these access rights more deeply, one needs to understand their respective scopes, on the other hand, it is necessary to clarify the respective programming ideas, for example, the private performance of the packaging ideas, public performance of the concept of interface-oriented programming.
For example:
Public classtest{ Public Static voidMain (string[] args) {Human Aperson=NewHuman (160); System.out.println (Aperson.getheight ()); Aperson.growheight (170); System.out.println (Aperson.getheight ()); Aperson.repeatbreath (100); }}classhuman{/*** Constructor*/ PublicHuman (inth) { This. Height =h; System.out.println ("I ' m Born"); } /*** Accessor*/ Public intgetheight () {return This. Height; } /*** Mutator*/ Public voidGrowheight (inth) { This. Height = This. Height +h; } /*** encapsulated, for internal use*/ Private voidBreath () {System.out.println ("Hu...hu ..."); } /*** Call Breath ()*/ Public voidRepeatbreath (intRep) { inti; for(i = 0; i < rep; i++) { This. Breath (); } } Private intHeight//encapsulated, for internal use}
In the process of object-oriented design and programming, it is the design of the class in the final analysis. To design a class that not only satisfies current requirements but also meets future requirements, it is necessary to set appropriate access rights to the properties and behaviors in the class. Those should be private, those should be public, and those should be protected, etc., should be given reasonable planning and layout, one to ensure the function of the program requirements, and secondly to reflect the program's extended performance, Sunline to show the program-friendly nature. In short, the program should serve the user better. Access rights play an important role in such ambitious goals.
Packaging for Java