我正在尝试创建一个简单的日期类。我的教授还希望我们在date类中包含我们自己的.equals方法,该方法应该比较两个对象。我的问题是,除非我比较完全相同的对象,即使它们的值相同,我的方法也会返回false。
这是我的司机:
public class Lab3Driver {
public static void main(String[] args) {
Date theDate = new Date(6, 30, 1995);
Date anotherDate = new Date(6, 30, 1995);
System.out.println(theDate.equals(anotherDate));
System.out.println(theDate);
System.out.println(anotherDate);
}
}
这是我的约会班级:
public class Date {
private int month;
private int day;
private int year;
public Date() // default no arg constructor
{
this.month = 1; // set to date I completed this class, for fun.
this.day = 26;
this.year = 2019;
}
public Date(int m, int d, int y) // normal constructor in case you want to initialize variables upon object declaration
{
this.month = m;
this.day = d;
this.year = y;
}
public int getMonth() {
return month;
}
public void setMonth(int month)
{
if (month >= 1 && month <= 12) // if else that checks and makes sure months are between 1 and 12
{
this.month = month;
}
else
{
System.out.println("Invalid month input. Months are between 1 and 12.");
}
}
public int getDay()
{
return day;
}
public void setDay(int day)
{
if (day >= 1 && day <= 31) // if else that checks and makes sure days are between 1 and 31
{
this.day = day;
}
else
{
System.out.println("Invalid day input. Days are between 1 and 31.");
}
}
public int getYear()
{
return year;
}
public void setYear(int year) // year can be set to anything, in the case that this program is used for something
{ // other than the present day, as in a reference to the past or future
this.year = year;
}
public String toString() // to string in order to print out the date that is stored
{
String theDate = "The date is: " + this.month + "/" + this.day + "/" + this.year;
return theDate;
}
public boolean equals(Object that) // compares two objects and checks for null/type casting
{
if (this == that)
return true;
else if(that == null || that.getClass()!= this.getClass())
{
System.out.println("Null or type casting of argument.");
return false;
}
else
return false;
}
这种方法引起的问题我认为:
public boolean equals(Object that) // compares two objects and checks for null/type casting
{
if (this == that)
return true;
else if(that == null || that.getClass()!= this.getClass())
{
System.out.println("Null or type casting of argument.");
return false;
}
else
return false;
}
答案 0 :(得分:1)
这很正常,因为您写了
else {
return false;
}
因此,只要that
对象具有不同的引用,并且来自同一类,则可以在上面的else语句中返回false。
您应该实现代码而不是返回false,例如:
public boolean equals(Object that) // compares two objects and checks for null/type casting
{
if (this == that)
return true;
else if(that == null || that.getClass()!= this.getClass())
{
System.out.println("Null or type casting of argument.");
return false;
}
else
return this.year == that.getYear() && ...;
}
答案 1 :(得分:0)
if (this == that)
此行不比较对象。这只会验证您的对象是否在相同的内存空间中,基本上会询问它是否完全相同(指向相同的位置)。
如果要比较两个不同的对象,则两个不同的实例,例如
Date theDate = new Date(6, 30, 1995);
Date anotherDate = new Date(6, 30, 1995);
然后,您必须添加更多行代码来检查每个对象中每个变量中的每个值,或者重写'=='方法以使其比较值。
答案 2 :(得分:0)
还有其他事情要解决:
正如内特已经说过的那样,您要强调的是您正在比较的两个对象的各个领域。为此,您可以使用return year == that.getYear() && day == that.getDay() && mοnth == that.getMοnth()
。
但是等等!您的equals
会接受Object
。因此,我们不能使用这些方法。您可以通过两种方式解决此问题。
instanceοf
检查,然后将参数强制转换为Date
对象。Date
个对象的参数。通常,我会选择后者,因为如果您使用n-Date
这个对象,则会在出现错误时出现错误。但是,如果您进行了类型检查并在类型检查失败的情况下进行了例外检查,则如果您提供的参数不是Date
,那么在调用该方法之前,您可能永远不会犯错。
答案 3 :(得分:0)
您需要确保,如果您覆盖了 equals 方法,您还应该覆盖 hashCode 方法。
供您参考,请阅读部分 https://www.baeldung.com/java-equals-hashcode-contracts#hashcode
我已经为您完成了两个覆盖的方法。
@Override
public boolean equals(Object that) {
if (this == that)
return true;
if(!(that instanceof Date))
return false;
if(that == null || that.getClass()!= this.getClass())
return false;
Date anotherDate = (Date) that;
if(this.month == anotherDate.month
&& this.day == anotherDate.day
&& this.year == anotherDate.year)
return true;
return false;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (month ^ (month >>> 16));
result = prime * result + (int) (day ^ (day >>> 16));
result = prime * result + (int) (year ^ (year >>> 16));
return result;
}