Java 从对象中获取变量?--我不知道该怎么说
所以我正在为学校写一个程序,它包括两个时间对象(t1和t2)。有小时,分,秒。一种方法是比较两者是否相等。 在我的驱动程序文件中,它应该类似于“t1.equals(t2);”并比较两者。 在该方法中,我如何获得它,以便程序“知道”比较t1中的变量 这是我现在知道的,但这是在我意识到它不应该是“等于(t1,t2)”,而是应该是“t1,等于(t2);”Java 从对象中获取变量?--我不知道该怎么说,java,object,Java,Object,所以我正在为学校写一个程序,它包括两个时间对象(t1和t2)。有小时,分,秒。一种方法是比较两者是否相等。 在我的驱动程序文件中,它应该类似于“t1.equals(t2);”并比较两者。 在该方法中,我如何获得它,以便程序“知道”比较t1中的变量 这是我现在知道的,但这是在我意识到它不应该是“等于(t1,t2)”,而是应该是“t1,等于(t2);” 我只是不知道如何让程序知道,如果第一次调用第二次,应该比较哪两次?(如果有意义)。您正在实例上调用该方法,因此需要将该实例的成员与传递的参数的成员进
我只是不知道如何让程序知道,如果第一次调用第二次,应该比较哪两次?(如果有意义)。您正在实例上调用该方法,因此需要将该实例的成员与传递的参数的成员进行比较:
public boolean equals (Time other) {
return getSecs() == other.getSecs() &&
getMins() == other.getMins() &&
getHrs() == other.getHrs();
}
请注意,尽管上述方法可行,但您可能应该遵循java的标准并重写,处理时间
实例不能等于非时间
实例的对象的概念:
@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();
}
任何合理的IDE都可以为您自动生成一个重写的
equals(对象o)
和hashCode()
例如,我所做的只是定义三个int字段,然后让生成器执行rest。这允许您调用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;
}
}
这基本上就是你想要的:
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
}
}
这就是你的时间课的样子:
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;
}
}
equals方法继承自Java中所有对象的
Object
。但是,如果您想实现自己的比较,那么应该重写从对象继承的方法,如上图所示。t1.equals(t2)
和t2.equals(t1)
将返回相同的结果。您应该阅读这篇文章
听起来您想覆盖equals:布尔方法将返回true或false。不是返回值相等代码>给您一个错误?所有java对象都继承自java.lang.Object类,该类定义了public boolean equals(Object o)
方法。如果所有子类都要比较多个内存地址,则它们都应该重写此方法。更详细地讨论这个问题。
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;
}
}