设置对象等于彼此(java) [英] setting objects equal to eachother (java)

查看:205
本文介绍了设置对象等于彼此(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为Person的类,看起来像这样

So I have a class called Person which looks like this

public class Person {

    private String personName;


    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

}

和我所在的另一个班级创建对象人

and another class in which I am creating the object(s) person

public class IdanJavaTask {

    public static void main(String[] args) {

        Person p1 = new Person("Bob");
        System.out.println("p1 : " + p1);

        Person p2 = new Person("Joe");
        System.out.println("p2 :" + p2);

    }

}

到目前为止一切很好,我的打印声明是

p1:Bob

p2:Joe

so far everything is fine and my print statement is
p1: Bob
p2: Joe

现在我要创建一个新对象,p3并将其设置为等于p1
我的班级现在看起来像这样:

Now I want to create a new object, p3 and set it equal to p1 my class now looks like this:

public class IdanJavaTask {

    public static void main(String[] args) {

        Person p1 = new Person("Bob");
        System.out.println("p1 : " + p1);

        Person p2 = new Person("Joe");
        System.out.println("p2 :" + p2);

        Person p3 = new Person (p1);
        System.out.println("p3 equal to p1:" + p3.equals(p1));

    }

}

当我尝试为此,我收到以下错误消息:

when I try to do this I get the following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor Person(Person) is undefined

    at vehicleassignment.IdanJavaTask.main(IdanJavaTask.java:13)

我想我需要在main(Person)类中添加一个方法,但我不知道为什么或要添加什么?为什么我不能将对象设置为彼此相等?

I am thinking I will need to add a method to my main (Person) class, but I do not know why or what to add? why can't I just set the objects equal to eachother?

推荐答案

有两种方法可以解释将对象设置为等于彼此。

There are two ways to interpret "set the objects equal to each other".

一个是你想要 p1 p3 引用相同的对象。就像Clark Kent和Superman是同一个人的两个名字(参考)一样。这将通过以下方式实现:

One is that you want p1 and p3 to refer to the same object. Like how Clark Kent and Superman are two names (references) for the same person. This would be accomplished by:

Person p1 = new Person("Jim");
Person p3 = p1;

在这种情况下,如果发生任何事情 p1 ,同样的事情发生在 p3 。如果你杀死克拉克肯特,你已经杀死了超人(因为他们是同一个人)。 Java确定与 equals(Object o)方法的相等性 - 两个对象 a b 等于iff a.equals(b) b.equals(a) return 。使用基本对象的相等定义,这两个对象将是相同的,因此您不必担心这一点。

In this scenario, if anything happens to p1, that same thing has happened to p3. If you kill Clark Kent, you have killed Superman (as they are one and the same). Java determines equality with the equals(Object o) method - two objects a and b are equal iff a.equals(b) and b.equals(a) return true. These two objects will be equal using the base Object definition of equality, so you don't have to worry about that.

解释你的意思的另一种方法是创建一个 new person对象,它恰好是第一个人的精确副本。为了做到这一点,你必须在你的person类中添加另一个构造函数,它将一个人作为参数:

The other way to interpret your meaning is to create a new person object, which happens to be an exact copy of the first person. In order to do this, you'd have to add another constructor to your person class that takes a person as an argument:

public class Person {

    private String personName;

    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

    public Person(Person personToCopy){
        this.personName = personToCopy.personName;
    }

}

通过此设置,您可以执行此操作你正在做什么。

With this setup, you can do what you're doing in your main.

Person p1 = new Person("Bob");
Person p3 = new Person(p1); //Will have name Bob.

为了使 p1 p3 相等,我们必须教Person类使用其字段来检查相等性。我们可以通过覆盖类人员中的等于方法来实现此目的。

In order to make p1 and p3 equal, we have to teach the Person class to use its fields for checking equality. We can do this by overriding the equals method in class person.

public boolean equals(Object o){
    if(! (o instanceof Person)) return false; //a Person can't be equal to a non-person

    Person p = (Person) o;
    return personName == null && p.personName == null || personName.equals(p.personName);
}

每当我们覆盖等于方法,最好还覆盖 hashcode 方法,该方法为每个Object返回唯一的 int 。由于 Person 对象的唯一字段是其名称,我们可以简单地使用该哈希码。

Whenever we overwrite the equals method, it is good practice to also overwrite the hashcode method, which returns a unique int for each Object. Since the only field that a Person object has is its name, we can simply use that hashcode.

public int hashCode(){
    return personName == null ? 0 : personName.hashCode();
}

所有这些,我们的Person类看起来像这样:

So all together, our Person class looks like this:

public class Person {

    private String personName;

    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

    public Person(Person personToCopy){
        this.personName = personToCopy.personName;
    }

    public boolean equals(Object o){
        if(! (o instanceof Person)) return false; //a Person can't be equal to a non-person

        Person p = (Person) o;
        return personName == null && p.personName == null || personName.equals(p.personName);
    }

    public int hashCode(){
        return personName == null ? 0 : personName.hashCode();
    }
}

这篇关于设置对象等于彼此(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆