Advantages of using getter and setter in the "Java" class
Http://www.importnew.com/9716.html
Java has three main features: encapsulation, inheritance, and polymorphism.
And today, let me talk about one of the most important features: encapsulation.
First, attributes can be used to describe the characteristics of the same class of things, and methods describe the operations that a class of things may do. Encapsulation is the inclusion of commonalities (including attributes and methods) belonging to the same category in a class for ease of use.
1. Concept: encapsulation, also known as information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible independent entity, the data is protected within the abstract data type, as much as possible to hide internal details, leaving only a few external interfaces to make the external connection. The rest of the system is communicated and interacted with this abstract data type only through authorized operations that are wrapped outside of the data. That is, the user does not need to know the implementation details of an object's internal methods, but can access the object based on the external interface (object name and parameters) provided by the object.
2. Benefits: (1) achieve a professional division of labor. Once the code that implements a particular function is encapsulated into a separate entity, the programmer can invoke it when needed, thus achieving a professional division of labor. (2) Hide the information and implement the details. By controlling access permissions, you can hide information that you do not want the client programmer to see, such as a customer's bank password that needs to be kept secret and can only develop permissions for that customer.
When we set the properties of a variable, we typically encapsulate the data, which increases the data access limit and increases the maintainability of the program. The implementation method is: Use private to modify a variable, and then use setter method to set the value of the variable, and then use getter method to call the value of the variable.
[Java]View PlainCopy
- Public class student{
- private String number; //Student Study No.
- private String name; //Student name
- private int grade; Student Achievement
- Public Student () {
- }
- Public String GetNumber () {//Get the number with Get method ( same as below)
- return number;
- }
- public void Setnumber (String number) {//Use Set method to set numbers (hereinafter)
- This.number=number;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- This.name=name;
- }
- public int Getgrade () {
- return grade;
- }
- public void Setgrade (int grade) {
- This.grade=grade;
- }
- public static void Main (String agrs[]) {
- Student st=New Student ();
- St.setnumber ("010112233");
- St.setname ("Xiao Ming");
- St.setgrade (100);
- SYSTEM.OUT.PRINTLN ("study Number:" +st.getnumber () +"," +"Name:" +st.getname () +"," +"score:" +st.getgrade () + ". ");
- }
- }
When a setter is used to change the value of a data member, the operation must be triggered by the object itself.
When you use public to change the value of a data member, the operation can be triggered by any object
This is an object-oriented package, in short, its own data members, only visible to themselves, but also only their own to change its value
The encapsulation of the object,
Private is accessible only to the object itself, and not to any other object, including its subclasses and parent classes. Security is high, and other objects can only get or set the private property of the original object through its public method, Set,get.
Public other objects can be accessed, and security is not high.
/* Question: What are the benefits of using getter and setter in Java classes?
* defined as private to achieve data hiding and encapsulation;
And the set get method provides the interface between the class and the outside;
This is necessary in large-scale software, which facilitates the maintenance of code
To give an example,
A parent class has multiple subclasses (and even indirect subclasses) in which the subclass is not directly accessible to the private property of the parent class, and the set get method provided is necessary.
Admittedly, if the attribute of the parent class is declared as protected, it can be accessed directly in the subclass, but this method destroys the principle of hiding and encapsulating the data, and the key is not conducive to the maintenance of the code.
If a property in the parent class is renamed, the code that uses the property name in the subclass has a wide range of modifications, and the previous Private,set,get method is better.
It's a good programming habit to have a small range of changes to the code, which is generally used.
The role of getter and Setter in Java (reprint)