//请看红色部分  是什么意思呢?  请大家指教   谢谢了~package org.leegang.hrsystem.model;import java.io.Serializable;
import java.util.Date;public class Attend implements Serializable
{
private static final long serialVersionUID = 48L; //代表标识属性
    private Integer id;
//出勤日期
    private String dutyDay;
//打卡时间
    private Date punchTime;
//代表本次打卡是否为上班打卡
    private boolean isCome;
//本次出勤的类型
    private AttendType type;
//本次出勤关联的员工
    private Employee employee; //无参数的构造器
public Attend()
{
}
//初始化全部属性的构造器
public Attend(Integer id , String dutyDay ,
Date punchTime , boolean isCome ,
AttendType type , Employee employee)
{
this.id = id;
this.dutyDay = dutyDay;
this.punchTime = punchTime;
this.isCome = isCome;
this.type = type;
this.employee = employee;
} //id属性的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
} //dutyDay属性的setter和getter方法
public void setDutyDay(String dutyDay)
{
this.dutyDay = dutyDay;
}
public String getDutyDay()
{
return this.dutyDay;
} //punchTime属性的setter和getter方法
public void setPunchTime(Date punchTime)
{
this.punchTime = punchTime;
}
public Date getPunchTime()
{
return this.punchTime;
} //isCome属性的setter和getter方法
public void setIsCome(boolean isCome)
{
this.isCome = isCome;
}
public boolean getIsCome()
{
return this.isCome;
} //type属性的setter和getter方法
public void setType(AttendType type)
{
this.type = type;
}
public AttendType getType()
{
return this.type;
} //employee属性的setter和getter方法
public void setEmployee(Employee employee)
{
this.employee = employee;
}
public Employee getEmployee()
{
return this.employee;
} //根据employee、isCome、dutyDay来重写equals方法
public boolean equals(Object obj)
    {
if (this == obj)
{
return true;
}
if (obj != null &&
obj.getClass() == Attend.class)
{
Attend attend = (Attend)obj;
return getEmployee().equals(attend.getEmployee())
&& getDutyDay().equals(attend.getDutyDay())
&& getIsCome() == attend.getIsCome();
}
return false;
} public int hashCode ()
    {
        if (getIsCome())
        {
    return dutyDay.hashCode() + 
29 * employee.hashCode() + 1;
        }
return dutyDay.hashCode() + 
29 * employee.hashCode();
}

}

解决方案 »

  1.   

    我查了相关资料了解了为什么要用hashCode但是为什么这里这个方法要乘以29然后加1呢?   其他的数字行么? 这个不怎么理解
      

  2.   

    我也不懂
    只知道用覆盖的hashCode()方法
      

  3.   

    hashCode()方法表示对象在内存上的格式。 为什么要剩以29然后再+1的原因就不大清楚了。
      

  4.   

    数字是随便的,只是为了产生不同的hashcode而已
      

  5.   

    小弟愚昧~   那为什么getIsCome()为真的时候返回结果是dutyDay.hashCode() + 
    29 * employee.hashCode() + 1呢?而不符合条件时就不需要加1呢?
      

  6.   

    很简单,因为它的equals方法用到了dutyDay, emplyee和isCome,这三个属性随便哪个不同都需要产生不同的hashcode,所以他根据这三个属性进行组合产生不同的hashcode而已,至于 dutyDay.hashCode() + m * employee.hashCode() + n,这里的m和n是你随便选的,无所谓,只要不超过int的范围就行
      

  7.   


    嗯   那就是说随便什么组合都行对吧  不一定是 dutyDay.hashCode() + m * employee.hashCode() + n这个组合  明白了~但是为什么是这3个属性呢?  呵呵  再挑属性的时候有什么注意的么?非常谢谢你~
      

  8.   

    dutyDay.hashCode() + 
    29 * employee.hashCode() hashcode是表示对象的唯一
    员工在什么时候是否签到
    就像数据库中的复合主键一样
    所以要
    dutyDay.hashCode() + 
    29 * employee.hashCode() 

    +1是表示来签到
      

  9.   

    这3个属性任意一个改变了~   这个类的hashCode()指向的内存地址就改变了是么?不知道我这样理解对不对~  呃```大家来说说 谢谢大家先
      

  10.   

    看来你没有了解为什么要重写hashcode方法,建议你去看看http://hi.baidu.com/lkdlhw_2000/blog/item/c28a044a7ddeab2208f7ef41.html
      

  11.   

    是为了更快的检索出类的不同对象的内存地址~  理解的应该差不多了吧  呵呵谢谢li_d_s咯~  有Q么?  加我行么?   87547255
      

  12.   

    如果一个类中没有成员属性是不是意味着不能重写hashcode()方法?如果可以应该怎么写?