The role of getter and Setter in Java (reprint)

Source: Internet
Author: User

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
  1. Public class student{
  2. private String number; //Student Study No.
  3. private String name; //Student name
  4. private int grade; Student Achievement
  5. Public Student () {
  6. }
  7. Public String GetNumber () {//Get the number with Get method ( same as below)
  8. return number;
  9. }
  10. public void Setnumber (String number) {//Use Set method to set numbers (hereinafter)
  11. This.number=number;
  12. }
  13. Public String GetName () {
  14. return name;
  15. }
  16. public void SetName (String name) {
  17. This.name=name;
  18. }
  19. public int Getgrade () {
  20. return grade;
  21. }
  22. public void Setgrade (int grade) {
  23. This.grade=grade;
  24. }
  25. public static void Main (String agrs[]) {
  26. Student st=New Student ();
  27. St.setnumber ("010112233");
  28. St.setname ("Xiao Ming");
  29. St.setgrade (100);
  30. SYSTEM.OUT.PRINTLN ("study Number:" +st.getnumber () +"," +"Name:" +st.getname () +"," +"score:" +st.getgrade () + ".  ");
  31. }
  32. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.