What is Getters, setters, equals and toString for this program. How to write it
public class People {
// Code to create a random set of people omitted
public Set getAllPeople() {
return people;
}
public void setPerson(Person person) {
if (person.getId() == -1) {
person.setId(getNextId());
}
people.remove(person);
people.add(person);
}
public void deletePerson(Person person) {
people.remove(person);
}
private Set people = new HashSet();
}
public 开发者_如何学Goclass Person
{
private int id;
private String name;
private String address;
private float salary;
// Getters, setters, equals and toString omitted
}
While looking after the DWR website i found this example.It states that they omitted Getters, setters, equals and toString. How to write those for this program. I wish to run this program and see. Any Suggestions Please. Help out..
Getters and Setters are used to retrieve your "private" variables ( = variables visible inside the class they are defined only), from outside the class.
For instance:
private String name;
would have a getter like this:
public String getName() {
return name;
}
And a setter like this:
public void setName(String name) {
this.name = name;
}
(you could use "protected" if you only wanted this variable to be visible in the package, and not in the whole project).
the toString() method is here if you want to display some information about your object, which might be useful from a debugging point of view.
The equals method would be used to know how you want to compare to objects of Person type (by ids only for instance). Have a look at this link to have more info on what is equals.
As RonK suggested, be sure to implement hashCode if you do implement equals, they go together, and have to use the same fields (part of the contract).
The rule is that if:
objectA.equals(objectB) returns true
then
objectA.hashCode() has to be equal to objectB.hashCode()
for each property in Person class you need to define 2 methods
for example id:
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
and you need to override equals and hashcode method to put your own condition for equality
public boolean equals(Object that) {
if (that == null) {
return false;
}
if (!(that instanceof Person)) {
return false;
}
return this.id == ((Person) that).id;
}
public int hashCode() {
return id * 17;
}
public class Person
{
//Id should be unique
private int id;
private String name;
private String address;
private float salary;
public Person(int id, String name, String address, float salary)
{
this.id = id;
this.name = name; //Maybe check for null
this.address = address; //Maybe check for null
this.salary = salary; //Maybe check for > 0
}
public int getId()
{
return id;
}
//No setID() - do you want that? you properly shouldn't
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address; //Maybe check for null
}
public float getSalary()
{
return salary;
}
public setSalary(float salary)
{
this.salary = salary;
}
//A person is equal if they have the same ID
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Person person = (Person)obj;
return person.id == id;
}
@Override
public int hashCode()
{
return id;
}
//Just returns the name but you could return more details
@Override
public String toString()
{
return name;
}
}
Added hashCode
which is essential - especially if you use it in a HashSet
.
精彩评论