Created
October 31, 2014 23:14
-
-
Save torifat/353d45b1058b93a9ec85 to your computer and use it in GitHub Desktop.
Java Person Class
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 implements Serializable { | |
private final String firstName; | |
private final String lastName; | |
public Person(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public Person withFirstName(String firstName) { | |
return new Person(firstName, lastName); | |
} | |
public Person withLastName(String lastName) { | |
return new Person(firstName, lastName); | |
} | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
Person person = (Person) o; | |
if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) { | |
return false; | |
} | |
if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) { | |
return false; | |
} | |
return true; | |
} | |
public int hashCode() { | |
int result = firstName != null ? firstName.hashCode() : 0; | |
result = 31 * result + (lastName != null ? lastName.hashCode() : 0); | |
return result; | |
} | |
public String toString() { | |
return "Person(" + firstName + "," + lastName + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment