为什么这个代码传入一个已有引用的实例被判定和集合中的元素不同?

为什么这个代码传入一个已有引用的实例被判定和集合中的元素不同?

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]


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,1.在此练习中两种方式都是可以的,不过同学需要注意二者的区别: getClass() != obj.getClass()是判断obj是Student类型。而obj instanceof Student中obj可以是Student子类的对象。所以同学需要根据他们的不同应用去使用。

2. 两个浮点型的数据,是可以使用等号(==)进行判断。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

好帮手慕阿慧 2020-04-29 17:16:47

同学你好,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;
}

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 notFoundMoneyE #1
    getclass()!=obj.getclass() 是为了判断obj是否是我们想要的Student的实例,那为什么不能用instanceof关键字呢
    2020-04-29 20:21:51
  • 提问者 notFoundMoneyE #2
    还有我比较两个float的值,能不能直接用等号?浮点数好像不是精确的,除了两个值的差小于某个值来判断还有没有其他方法?
    2020-04-29 20:24:33
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师