所以我正在为学校写一个程序,它涉及两个时间对象(t1和t2)。有小时,分钟,秒。一种方法是比较两者,看它们是否相等。 在我的驱动文件中,它应该像" t1.equals(t2);"并比较两者。 在方法中如何获得它,以便程序"知道"比较来自t1的变量?
这是我现在所拥有的,但这是在我意识到它不应该是"等于(t1,t2);"但应该是" t1.equals(t2);"
public boolean equals(Time one, Time two)
{
boolean areEqual=true;
int timeOneSecs=one.getSecs();
int timeOneMins=one.getMins();
int timeOneHrs=one.getHrs();
int timeTwoSecs=two.getSecs();
int timeTwoMins=two.getMins();
int timeTwoHrs=two.getHrs();
if (timeOneSecs!=timeTwoSecs)
{
areEqual=false;
}
if (timeOneMins!=timeTwoMins)
{
areEqual=false;
}
if (timeOneHrs!=timeTwoHrs)
{
areEqual=false;
}
return areEqual;
}
我只是不确定如果第一个呼叫第二个,如何让程序知道要比较两次? (如果这是有道理的)。
答案 0 :(得分:0)
您正在实例上调用该方法,因此您需要将该实例的成员与传递的参数进行比较:
public boolean equals (Time other) {
return getSecs() == other.getSecs() &&
getMins() == other.getMins() &&
getHrs() == other.getHrs();
}
请注意,尽管上述方法可行,但您应该遵循java的标准并覆盖Object#equals(Object)
,处理Time
实例不能等于不是&#39的对象的概念; ta Time
实例:
@Override
public boolean equals (Object o) {
if (!(o instanceof Time)) {
return false;
}
Time other = (Time)o;
return getSecs() == other.getSecs() &&
getMins() == other.getMins() &&
getHrs() == other.getHrs();
}
答案 1 :(得分:0)
任何合理的IDE都可以为您自动生成被覆盖的equals(Object o)
和hashCode()
。
例如,我所做的就是定义三个int字段,然后让生成器休息。这允许您拨打t1.equals(t2)
,反之亦然。
public class Time {
private int hrs, mins, secs;
public Time(int hrs, int mins, int secs) {
this.hrs = hrs;
this.mins = mins;
this.secs = secs;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Time time = (Time) o;
if (hrs != time.hrs) return false;
if (mins != time.mins) return false;
return secs == time.secs;
}
@Override
public int hashCode() {
int result = hrs;
result = 31 * result + mins;
result = 31 * result + secs;
return result;
}
}
答案 2 :(得分:0)
这基本上是你想要的:
public class Main{
public static void main(String []args){
Time t1 = new Time(1,2,3);
Time t2 = new Time(1,2,3);
Time t3 = new Time(1,2,4);
Object t4 = new Object();
System.out.println(t1.equals(t2)); //Writes true
System.out.println(t1.equals(t1)); //Writes true
System.out.println(t1.equals(t3)); //Writes false
System.out.println(t1.equals(t4)); //Writes false
System.out.println(t1.equals(null)); //Writes false
}
}
这就是你的Time类的样子:
public class Time
{
private int sec;
private int min;
private int hour;
public Time(int sec, int min, int hour){
this.sec = sec;
this.min = min;
this.hour = hour;
}
@Override
public boolean equals(Object obj){
//If you compare to null, obviously they are not the same.
if (obj == null) return false;
//If they point to the same address in the heap they must be the same.
if (this == obj) return true;
//If you compare it to something that is not a time object, they must be different
if (!(obj instanceof Time)) return false;
Time t2 = (Time)obj;
return this.sec == t2.getSec() &&
this.min == t2.getMin() &&
this.hour == t2.getHour();
}
public int getSec(){
return this.sec;
}
public int getMin(){
return this.min;
}
public int getHour(){
return this.hour;
}
}
对于Java中的所有对象,equals方法继承自Object
。但是,如果要实现自己的比较,则应覆盖从Object继承的方法,如上所示。