用Java编写用于继承的equals()方法 [英] Writing an equals() method for Inheritance in Java
问题描述
我在做一个继承问题,除布尔部分外,我已完成所有工作.我的代码可以编译,但是当比较名称,生日和ssn时,只会显示为false.
Im doing an inheritance question and I have everything completed except the Boolean part. My code will compile, but when comparing the name, birthday, and ssn, it will only come out as false.
例如,我输入:
- 吉姆
- 6 30 2001
- 123456789
- 吉姆
- 6 30 2001
- 123456789
输出将为假.
public class Person
{
private String name;
private Date birthday;
private int ssn;
public Person(String name, Date birthday, int ssn)
{
this.name = name;
this.birthday = birthday;
this.ssn = ssn;
}
public String getName()
{
return name;
}
public Date getBirthday()
{
return birthday;
}
public int getSSN()
{
return ssn;
}
/* I already called a dates toString() method for birthday */
public String toString()
{
return name + " has birthday " + birthday + " and SSN " + ssn;
}
public boolean equals(Object otherObj)
{
if(otherObj == null)
return false;
else if(otherObj.getClass() != this.getClass())
return false;
else
{
Person otherP = (Person)otherObj;
if(otherP.name.equals(this.name) &&
otherP.birthday == this.birthday &&
otherP.ssn == this.ssn)
return true;
else
return false;
}
}
}
推荐答案
我认为问题出在这里:
if(otherP.name.equals(this.name) &&
otherP.birthday == this.birthday &&
otherP.ssn == this.ssn)
上面的代码是您的主要代码,用于检查两个Person
对象是否相等.您说当您提供两个具有完全相同的生日,名称和SSN的对象时,就会发生问题.我不确定您的输入代码如何创建对象,但是据我所知,它可能是为每个生日创建一个新的日期.
The code above is your main code for checking whether or not two Person
objects are equal. You say your issue occurs when you supply two objects with the exact same birthdays, names, and SSNs. I'm not sure how your input code is creating the objects, but from what I can tell it's probably creating a new Date for every birthday.
==
运算符和equals
方法之间存在重要区别.任何类型的变量都包含一个值.对于原始数据类型(例如int
),变量的值是其实际存储的值.但是,对于对象变量(例如String
,Date
),该值实际上是对对象的引用.如果两个对象变量引用堆上的同一对象,则它们将具有相同的值;否则,它们将具有相同的值.如果它们引用堆上的不同对象,则它们将具有不同的值.
There is an important difference between the ==
operator and the equals
method. A variable of any type contains a value. For primitive data types (e.g. int
), the value of the variable is the value it's actually storing. However, for an object variable (e.g. String
, Date
) the value is actually a reference to an object. If two object variables refer to the same object on the heap, they will have the same value; if they refer to different objects on the heap, they will have different values.
==
运算符仅检查变量的值,仅此而已.与原语一起使用时,它会检查存储在变量中的值是否相同,这是正确的,因为这些值是直接存储的.将其与对象变量一起使用时,它将检查两个变量是否引用堆上的同一对象.由于变量的值实际上是引用,因此当您检查两个值或引用是否相同时,将得出结果.
The ==
operator just checks the value of the variable, nothing else. When using it with primitives, it checks to see if the values stored in the variables are the same, which is proper because the values are stored directly. When using it with object variables, it checks to see if the two variables refer to the same object on the heap. Since the values of the variables are in fact references, this is the result when you check if the two values, or references, are the same.
另一方面,equals
方法实际上(应该)进一步检查两个对象是否相等,因为它们的状态相同.例如,它可以检查两个Date
对象是否表示相同的日期. Java无法神奇地判断两个不同的对象是否相等-该类必须自己实现equals
方法. (否则,它求助于默认实现,在该实现中,它仅检查引用.)
The equals
method, on the other hand, actually (is supposed to) go check further if two objects are considered equal, in that their states are the same. For example, it can check if two Date
objects represent the same date. Java cannot magically tell if two different objects are equal - the class has to implement the equals
method itself. (Otherwise, it resorts to a default implementation where it just checks the references.)
在上面的代码示例中,您在原语ssn
上正确使用了==
运算符(因为它是一个原语),并且在对象name
上正确使用了equals
方法(由于在每个Person
可以不同,但是代表相同的字符串).但是,您正在对象变量birthday
上使用==
运算符,该运算符只是检查两个被比较的Person
对象是否具有相同的Date
对象.可能是因为调用代码只是在每个输入上创建一个新的Date
对象,所以检查失败,因为两个Person
对象为birthday
具有两个不同的对象.只需将其更改为equals
方法调用,您的代码就可以使用.
In your code sample above, you are using the ==
operator properly on the primitive ssn
(since it is a primitive), and the equals
method properly on the object name
(since the references in each Person
could be different, but represent the same string). However, you are using the ==
operator on the object variable birthday
, which simply checks if the two Person
objects being compared have the same Date
object. Likely, since your calling code is just creating a new Date
object on each input, the check fails because the two Person
objects have two different objects for birthday
. Just change that to an equals
method call and your code should work.
这篇关于用Java编写用于继承的equals()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!