When creating a class that instantiates an object in Java
-** Constructor **
It seems that you write it roughly, but it is troublesome to write code every time.
Conclusion: Let's generate it automatically with ** command + n ** without touching it! !!
Let's take the Person class, which has a person's name, age, and ID as member variables, as an example.
** Person class **
public class Person {
private int PersonId;
private String PersonName;
private int PersonAge;
public Person(int PersonId, String PersonName, int PersonAge) {
this.PersonId = PersonId;
this.PersonName = PersonName;
this.PersonAge = PersonAge;
}
public Person() {
}
public int getPersonId() {
return PersonId;
}
public String getPersonName() {
return PersonName;
}
public int getPersonAge() {
return PersonAge;
}
public void setPersonId(int PersonId) {
this.PersonId = PersonId;
}
public void setPersonName(String PersonName) {
this.PersonName = PersonName;
}
public void setPersonAge(int PersonAge) {
this.PersonAge = PersonAge;
}
}
-** Constructor, create default constructor **
public Person(int PersonId, String PersonName, int PersonAge) {
this.PersonId = PersonId;
this.PersonName = PersonName;
this.PersonAge = PersonAge;
}
public Person() {
}
Select Constructor with command + n
** Select a member variable ** and click OK to automatically generate the constructor. If ** no variables are specified **, the default constructor will be automatically generated.
-** Create Getter / Setter **
public int getPersonId() {
return PersonId;
}
public String getPersonName() {
return PersonName;
}
public int getPersonAge() {
return PersonAge;
}
public void setPersonId(int PersonId) {
this.PersonId = PersonId;
}
public void setPersonName(String PersonName) {
this.PersonName = PersonName;
}
public void setPersonAge(int PersonAge) {
this.PersonAge = PersonAge;
}
Select Getter / Setter with command + n Select a member variable and click OK to automatically generate Getter / Setter for each variable.
This is the easiest because you don't have to write get (), set () many times anymore.
Thank you very much.
Recommended Posts