用java编写继承,用Java编写用于继承的equals()方法

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.

For example, I inputted:

Jim

6 30 2001

123456789

Jim

6 30 2001

123456789

The output will be false.

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;

}

}

}

解决方案

I think the problem lies here:

if(otherP.name.equals(this.name) &&

otherP.birthday == this.birthday &&

otherP.ssn == this.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.

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.

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.)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值