Created
May 31, 2015 01:50
-
-
Save diegozr1/8896bd4bf1eb74dc5fb1 to your computer and use it in GitHub Desktop.
A class with getters and setters in Java in order to use it as an Object in an Array of them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
//attributes of the class | |
private String FirstName, | |
LastName; | |
private Integer Age, | |
Phone; | |
//Constructors for this class | |
public Person(){ | |
this.FirstName = null; | |
this.LastName = null; | |
this.Age = null; | |
this.Phone = 0; | |
} | |
public Person(String FirstName, String LastName, Integer Age, Integer Phone){ | |
this.Firstname = Firstname; | |
this.LastName = LastName; | |
this.Age = Age; | |
this.Phone = Phone; | |
} | |
//Methods for the full name | |
public void setName(String FirstName, String LastName){ | |
this.FirstName = FirstName; | |
this.LastName = LastName; | |
} | |
public String getName(){ | |
return this.FirstName + " " + this.LastName; | |
} | |
//Setters for the attributes | |
public void setFirstName(String FirstName){ | |
this.Firstname = FirstName; | |
} | |
public void setLastName(String LastName){ | |
this.LastName = LastName; | |
} | |
public void setAge(Integer Age){ | |
this.Age = Age; | |
} | |
public void setPhone(Integer Phone){ | |
this.Phone = Phone; | |
} | |
// Getters for each of them | |
public String getFirstName(){ | |
return this.FirstName; | |
} | |
public String getLastName(){ | |
return this.LastName; | |
} | |
public Integer getAge(){ | |
return this.Age; | |
} | |
public Integer getPhone(){ | |
return this.Phone; | |
} | |
//Overwriting the method toString | |
public String toString(){ | |
return this.Firstname + " " + this.LastName + " | " + " Age: " this.Age + " | " + " Phone: " + this.Phone; | |
} | |
// The use of the keyword "this" is very important because it refers to the attribute of the object not a variable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment