简体   繁体   English

此程序的Getter,setter,equals和toString是什么。 怎么写

[英]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 class 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. 在照顾DWR网站时,我发现了这个例子,它声明他们省略了Getters,setters,equals和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. Getter和Setter用于从类外部检索“私有”变量(=仅在类内部可见的变量)。

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. 如果要显示有关对象的某些信息,则toString()方法在此处,从调试的角度来看这可能很有用。

The equals method would be used to know how you want to compare to objects of Person type (by ids only for instance). equals方法将用于了解您如何与Person类型的对象进行比较(仅以id为例)。 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). 正如RonK所建议的,如果您确实实现了equals,则一定要实现hashCode,它们将并在一起,并且必须使用相同的字段(合同的一部分)。

The rule is that if: 规则是:

objectA.equals(objectB) returns true

then 然后

objectA.hashCode() has to be equal to objectB.hashCode()
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 . 添加了必不可少的hashCode特别是如果在HashSet使用它。

for each property in Person class you need to define 2 methods 对于Person类中的每个属性,您需要定义2个方法

for example id: 例如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 并且您需要重写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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM