为什么equals 重新之后结果还是false?
代码1:
public void testListContains(){ //Course course= (Course) coursesToSelect.toArray()[1]; List list=new ArrayList(coursesToSelect); Course course= (Course) list.get(0); System.out.println("获取到的学生ID是:"+course.id+" 获取到的学生姓名是:"+course.name); System.out.println("集合中是否包含课程"+course.name+":"+coursesToSelect.contains(course)); Course course2=new Course(course.id,course.name); System.out.println("新创建的课程是:" +course2.id+","+course2.name); System.out.println("集合中是否包含课程"+course2.name+":"+coursesToSelect.contains(course2)); //System.out.println("一共有 "+coursesToSelect.size()+" 门课程"); } 代码2: @Override public boolean equals(Object obj){ if(this==obj) return true; if(obj==null) return false; if(!(obj instanceof Course)){ return false; } Course course= (Course) obj; if(this.name==null){ if(course.name==null) return true; else return false; }else{ if(this.name.equals(course.name)){ return true; }else{ return false; } } //return true; }