为什么这个代码传入一个已有引用的实例被判定和集合中的元素不同?
public class Student {
private int stuid;
private String name;
private float score;
public int getstuid(){
return this.stuid;
}
public String getname(){
return this.name;
}
public float getscore(){
return this.score;
}
public void setstuid(int stuid){
this.stuid=stuid;
}
public void setname(String name){
this.name=name;
}
public void setscore(float score){
this.score=score;
}
public Student(){}
public Student(int stuid,String name,float score){
this.setstuid(stuid);
this.setname(name);
this.setscore(score);
}
public String toString(){
return "[学号:"+this.getstuid()+",姓名:"+this.getname()+",成绩:"+this.getscore()+"]";
}
//------------------------------------------------------------------------------------
public int hashCode() {
return super.hashCode();
}
//--------------------------------------------------------------------------------------
public boolean equals(Object obj){
if(this==obj){
return true;
}
Student s1;
if(obj instanceof Student){
s1=(Student)obj;
}else{
return false;
}
if(this.getstuid()==s1.getstuid()&&this.getname().equals(s1.getname())&&this.getscore()-s1.getscore()<0.01f){
return true;
}
return false;
}
}
//下面为运行类:
package setTest;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student(3,"william",65.0f);
Student s2=new Student(1,"tom",87.0f);
Student s3=new Student(2,"lucy",95.0f);
//将Student类的对象添加到集合中
Set setone=new HashSet();
setone.add(s1);
setone.add(s2);
setone.add(s3);
Iterator it=setone.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("=========================");
Student s4=s1;
Student s5=new Student(1,"tom",87.0f);
setone.add(s4);
setone.add(s5);
it=setone.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
//下面为运行结果:
[学号:1,姓名:tom,成绩:87.0]
[学号:3,姓名:william,成绩:65.0]
[学号:2,姓名:lucy,成绩:95.0]
=========================
[学号:1,姓名:tom,成绩:87.0]
[学号:1,姓名:tom,成绩:87.0]
[学号:3,姓名:william,成绩:65.0]
[学号:2,姓名:lucy,成绩:95.0]
正在回答
同学你好,1.在此练习中两种方式都是可以的,不过同学需要注意二者的区别: getClass() != obj.getClass()是判断obj是Student类型。而obj instanceof Student中obj可以是Student子类的对象。所以同学需要根据他们的不同应用去使用。
2. 两个浮点型的数据,是可以使用等号(==)进行判断。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
同学你好,Student类的hashCode()和equals()方法写错了,导致根据学号和姓名判断是不是同一个对象错误。
如下:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + Float.floatToIntBits(score);
return result;
}
@Override
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 (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Float.floatToIntBits(score) != Float.floatToIntBits(other.score))
return false;
return true;
}如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星