麻烦大家帮忙看看,thx!!!
//这是测试方法 public void testContains(){ Course course = courseToSelect.get(0); System.out.println("取得备选课程:"+course.name); System.out.println("备选课程中是否包含本课程:"+course.name+", "+courseToSelect.contains(course)); Course course2 = new Course(course.id,course.name); System.out.println("新建课程:"+course2.name); System.out.println("备选课程中是否包含本课程:"+course2.name+", "+courseToSelect.contains(course2)); System.out.println("请输入要查询的课程名称:"); String name = sc.next(); Course course3 = new Course(); course3.name = name; System.out.println("备选课程中是否包含本课程:"+course3.name+", "+courseToSelect.contains(course3)); } //这是Course类里面重写的equals方法 @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if(!(obj instanceof Course)) return false; Course other = (Course) obj; if (this.id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (this.name == null) { if (other.name != null) return false; } else if (!this.name.equals(other.name)) return false; return true; }
这里为什么会是false呢?