为什么我写了hashcode方法后还是返回false?
代码
public class Class {
String id;//课程
String name;
public String getId() {
return id;
}
public String getName() {
return name;
}
public void set(String name,String id){
this.id=id;
this.name =name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Class))
return false;
Class other = (Class) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
/////////////////
public void stuCourse(){//测试学生选择课程
System.out.println("测试学生选择课程2");
Student stu=new Student();
stu.set("小明","1");
System.out.println("***欢迎学生"+stu.name+"选课***");
Scanner input=new Scanner(System.in);
while(true){
System.out.println("请问您要选择多少门课程?");
int a=input.nextInt();
if(a!=0){
if(a<=courseClass.size()){
for(int i=0;i<a;i++){
System.out.println("请输入课程id:");
String kcid=input.next();
for(Class cl:courseClass){
if(cl.id.equals(kcid)){
stu.cla.add(cl);//保存选择的课程
}
}
}
}else{
System.out.println("课程大于课程总量!");
continue;
}
}else{
System.out.println("至少选1门课程!!");
continue;
}
xuanze();
break;
}
}
public void xuanze(){//输出总共选择的课程
for(Class c:Student.cla){
System.out.println("选择了 课程"+c.id+":"+c.name);
}
}
//set的contains 方法
public void testSetContains(){
System.out.println("测试学生已选择的课程名称:");
String names=input.next();
Class cla3 = new Class();
cla3.name=names;
System.out.println("取得课程:"+names);
System.out.println("是否包含课程:"+cla3.name+":"+student.cla.contains(cla3));
}