JAVA Get Set Usage

Source: Internet
Author: User

For Java beginners, both the set and get methods seem familiar, and these two methods are the basic usage of Java into, and also the two methods that occur at a very high frequency.

In order for Java beginners to better understand the use and significance of these two methods, today I would like to talk about their understanding of these two methods, if you are still confused about these two methods or even completely do not know what these two methods are doing, then you see this article is very useful. If you're already familiar with both the set and get methods, you don't have to read this article at all. This article is written for beginners.

Let's take a look at the surface meaning of set and get two words, set is the meaning of the set, and get is the meaning of getting, as the name implies, these two methods are to set up and obtain data. Also, when using the set and get methods in a class, it is the method name, such as Setage () and Getage (), that is followed by a set and get that follows certain words to form a specific meaning, such as set age and acquisition age.
Then let's look at the closeness and security of Java object-oriented programming. Enclosing is the closed operation of the domain variables in the class, which is to decorate them with private, so that other classes cannot access the variable. So we're enclosing these variables inside the class, which improves the security of the data, what do we do when we want to manipulate these domain variables? We can use two methods, the first is the public way of the constructor (or constructor), the object of an instantiation of the variable is assigned value. The second is through the set and get methods mentioned above, here I give a specific example, I define a person class, in which there are two private domain variables name, age, and then I define SetName (), GetName (), Setage (), Getage () These four methods, through the four methods to implement the operation of the name and age. In this way, instead of directly manipulating the domain variables in the person class, I use the set and get methods to manipulate these variables indirectly, which can improve the security of domain variables while guaranteeing the encapsulation of domain variables.

Finally, the set and get methods are used in scenarios where both the set and get methods operate on private domain variables, so most are used in class entities that contain specific attributes.
Now that you understand the set and get methods, actually these two methods are just the generic name of the Setxxx and GetXXX methods in the class.
In the future development process, as long as you carefully understand, combined with some of the macro-knowledge in Java. The set and get methods are easy to understand, and perhaps you will also learn some of the other important theories in Java.

Package cn.itcast.domain;

Import Java.util.Date;

public class User {

Private String username;
private String password;
Private String gender;
Private Date birthday;
Private String Email;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String Getgender () {
return gender;
}
public void Setgender (String gender) {
This.gender = gender;
}
Public Date Getbirthday () {
return birthday;
}
public void Setbirthday (Date birthday) {
This.birthday = Birthday;
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}

}

User U = new user ();
U.setxxx ();
U.getxxx ();
You want to give him a value to use U.SETXXX (); A value for the object of this class is a get

JAVA Get Set usage

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.