有人可以告诉我为什么它会回归"不等于"?我很长时间没有搜索这个...我认为我的哈希码或/和equals方法存在问题。
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.setFirstName("Kamil");
s1.setLastName("Witam");
s1.setMark(3.0);
s1.setAge(12);
Student s2 = new Student();
s2.setFirstName("Kamil");
s2.setLastName("Witam");
s2.setMark(3.0);
s2.setAge(12);
if( s1.equals(s2)){
System.out.println("równe");
}
else{
System.out.println("sraka");
}
}
}
我的hashCode:
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ( ( firstName == null ) ? 0 : firstName.hashCode() );
result = prime * result + ( ( lastName == null ) ? 0 : lastName.hashCode() );
long temp;
temp = Double.doubleToLongBits( mark );
result = prime * result + (int) ( temp ^ ( temp >>> 32 ) );
return result;
}
等于:
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
Student other = (Student) obj;
if ( age != other.age )
return false;
if ( firstName == null )
{
if ( other.firstName != null )
return false;
}
else
if ( !firstName.equals( other.firstName ) )
return false;
if ( lastName == null )
{
if ( other.lastName != null )
return false;
}
else
if ( !lastName.equals( other.lastName ) )
return false;
if ( mark != other.mark )
return false;
return true;
}
有人可以向我解释一下:/?感谢