package imooc;
public class Course
{
public String id;
public String name;
public Course(String id,String name)
{
this.name=name;
this.id=id;
}
@Override
public boolean equals(Object obj)
{
if(this==obj)return true;
if(obj==null)return false;
//Object Course;
if((obj instanceof Course)==false)return false;
Course cours=(Course)obj;
if(this.name.equals(cours.name)&&this.id.equals(cours.id))
{
return true;
}
else return false;
}
@Override
public int hashCode() {
String id = this.name + this.id + "";
return id.hashCode();
}
}
object对象中的 public boolean equals(Object obj),指示其他某个对象是否与此对象“相等”。这里的相等指的是比较的两方指向同一个对象
对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(就是同一块内存地址),此方法才返回 true;
在重写之后,最好重写hashcode方法。