这个程序的Getters,setter,equals和toString是什么。怎么写

时间:2010-08-20 18:20:48

标签: java dwr

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 class Person
{
private int id;
private String name;
private String address;
private float salary;

// Getters, setters, equals and toString omitted
}

在看DWR网站时我找到了这个例子。它说它们省略了Getters,setters,equals和toString。如何为这个程序写那些。我想运行这个程序,看看。任何建议请。帮忙..

3 个答案:

答案 0 :(得分:3)

Getters和Setter用于从类外部检索您的“私有”变量(=仅在其定义的类中可见的变量)。

例如:

private String name;

会有这样的吸气剂:

public String getName() {
    return name;
}

像这样的二传手:

public void setName(String name) {
    this.name = name;
}

(如果您只希望此变量在包中可见,而不是在整个项目中,则可以使用“protected”。)

如果要显示有关对象的一些信息,可以使用toString()方法,从调试的角度来看,这可能很有用。

equals方法将用于了解您希望如何与Person类型的对象进行比较(仅限id)。 请查看此link,了解有关等于什么的更多信息。

正如RonK建议的那样,一定要实现hashCode,如果你实现了equals,它们会一起使用,并且必须使用相同的字段(合同的一部分)。

规则是:if:

objectA.equals(objectB) returns true

然后

objectA.hashCode() has to be equal to objectB.hashCode()

答案 1 :(得分:2)

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;
    }
}

添加了hashCode,这是必不可少的 - 特别是如果您在HashSet中使用它。

答案 2 :(得分:2)

对于Person类中的每个属性,您需要定义2个方法

例如id:

public void setId(int id) {
   this.id = id;
}

public int getId() {
   return id;
}

并且您需要覆盖equals和hashcode方法以将您自己的条件置于等式

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;
}