1. What is packaging?
Encapsulation is to privatize attributes and provide public methods to access private attributes.
The method is to modify the visibility of attributes to limit access to attributes, and create a pair of getter methods and setter methods for each attribute to access these attributes.
For example: private String name;
The code copy is as follows:
public String getName(){
return;
}
public void setName(String name){
this.name=name;
}
2. Why do you need packaging?
Through encapsulation, data access restrictions on attributes can be achieved, while increasing the maintainability of the program.
Since the value method and the assignment method hide the implementation changes, it will not affect the reading or modifying the class of the attribute, avoiding large-scale modifications, and increasing the maintainability of the program.
3. What does this keyword mean?
Sometimes a method needs to refer to the object that calls it. For this purpose, java defines the keyword this. Simply put,
This is a reference that refers to itself within an object. An object can be referenced directly, and any conflicts of the same name occur between instance variables and local variables.
4. How to implement packaging and specific methods of packaging
(1) Modify the visibility of the attribute to limit access to the attribute.
(2) Create a pair of assignment methods and value methods for each attribute to access these attributes.
(3) In the assignment and value method, add restrictions on access to attributes.
The above is the entire content of this article, I hope you like it.
Please take some time to share the article with your friends or leave a comment. We will sincerely thank you for your support!