Encapsulation in Java
One: the concept of encapsulation
The object's attributes and operation (or function) are combined into a separate whole, and the inner implementation details of the object are hidden as much as possible, and the main embodiment is the reusability of the hidden and the code. In general, we understand the concept is not easy to understand, below we can use simple example code and easy-to-understand words to explain the above concepts. In fact, the class in Java is a good example of encapsulation, class is an abstract concept set, representing a common product, the class is defined by attributes and behavior (methods), such as one of us, although each individual is not the same, such as interest, height, appearance, character, etc., but the Some people will show common, such as all have age, height, eyes, mouth, will eat, will sleep and so on. So to abstract people's generality, to form a collection of concepts, we often define a person class, in fact, this is the case.
Second: Understanding of concepts and the "inner details of hidden objects" and "the emphasis on code reuse"
In understanding the concept of "inner details of hidden objects", we have to understand a concept that is the problem of accessing modifiers:
One of the basic concepts of Java object-oriented is to encapsulate the details and expose the interface. The Java language uses access control modifiers to control access to the methods and variables of classes and classes, exposing the interface to the consumer, but hiding the implementation details access control into four levels:
(1)public: classes, generic variables and methods that are modified with public, and any class within and outside the package (including subclasses and ordinary classes) can be accessed;
(2)protected: Protected modified class, generic variables and methods, any class within the package and those inheriting the subclass of the class can access (explained here later), protected focus on inheritance;
(3)default: If a class, generic variables, and methods do not have any modifiers (that is, none of the public, protected, and private), then their access rights are default (access rights are defaulted). Implied
classes, generic variables, and methods that recognize access, any class within the package (including subclasses that inherit the class) can access it, and no class outside the package will be able to access it (including subclasses that inherit the class outside of the package). default Focus Highlight Package ;
(4)private: class, generic variables and methods that are modified with private, only this class can access, and any class outside the package cannot access it.
Access level |
Access Control modifiers |
Similar |
Same package |
Sub-class |
Different packages |
Public |
Public |
√ |
√ |
√ |
√ |
be protected |
Protected |
√ |
√ |
√ |
-- |
Default |
No access control modifiers |
√ |
√ |
-- |
-- |
Private |
Private |
√ |
-- |
-- |
--
|
Let's look at a piece of code:
public class studentboke{
Property
private String name;
private String number;
private int age;
Setter and Getter method
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetNumber () {
return number;
}
public void Setnumber (String number) {
This.number = number;
}
is a function of name and number verification
public boolean Yangzheng (String name,string number) {
if (Name.equals ("Xiaoming") &&number.equals ("123")) {
return true;
}else{
return false;
}
}
This is a function of a
public void A () {
Verification of name and number before printing System.out.println ("This is a function")
Yangzheng ("A", "a");
System.out.println ("This is a function");
}
This is a B function.
public void B () {
In the print System.out.println ("This is a B function"), before the name and the number of the verification
Yangzheng ("B", "B");
System.out.println ("This is a B function");
}
}
We see that the age of the class above is the private access modifier and does not provide a setter and getter method, which means that in addition to accessing the age attribute within this class, there is no access to this age, the other two properties name, Number also uses private decoration, why can access outside the class, because they have set the public setter and getter method, although we can not use outside the class: New Studentboke (). Name to access the Name property, However, you can use new Studentboke (). GetName () to access the Name property, which is called using an already open interface, while the use of private embodies the hidden nature of encapsulation.
Another example of a hidden case that manifests encapsulation is the singleton design pattern:
1 Public classsingleton{2//define a private class variable to hold a singleton, the purpose of which is to refer to the external cannot directly get the variable, but to use the public method provided to get3Private StaticSingleton Singleton =NULL; 4//defines a private constructor that is used only within a class, or that an instance of a singleton can only be created inside a singleton class5Private Singleton(){} 6//defines a public, exposed method to return an instance of the class7 Public Static Singleton getinstance () {8if(Singleton = =NULL){ 9 Singleton =New Singleton();10 }11returnSingleton;12 }13}
In this example, we see that it has privatized a constructor (hidden), so it is not possible to use new Singleton () to create an instance of the new one, which can only be obtained using the GetInstance method.
When we learn Java, often heard to put a function encapsulated into a method, in fact, this is also the embodiment of a package, if the above code we have two functions, a function and B function, in the first two functions, there is a need to do a name and number verification, If we write this verification function separately in the A function and the B function, is this the case:
1 //This is a function of a2 Public voidA () {3 4 if(Name.equals ("a") &&number.equals ("a"))){5 return true;6}Else{7 return false;8 }9 TenSystem.out.println ("This is a function"); One } A
- //This is a B function . - Public voidB () { the - if(Name.equals ("B") &&number.equals ("B"))){ - return true; -}Else{ + return false; - } + ASystem.out.println ("This is a B function"); at}
This is not going to bring two problems one is the code redundancy is increased, another problem is to reduce the maintainability of the code, because I want to modify the Yanzheng method, if it is encapsulated as a method, then in the maintenance, as long as the maintenance of this place is possible. If you put it in a A-B two method, you should maintain two places.
Three: Through the above description, we can simply summarize the benefits of encapsulation
1. Good encapsulation can reduce coupling and improve maintainability.
2. You can have more precise control over member variables.
3. Hide the information and implement the details.
Understanding Java Basic Concepts----encapsulation