您好,欢迎访问一九零五行业门户网

equals、hashCode、toString方法重写

equals 方法重写的规则
判断一定相等(引用一致)
判断一定不等(类型不一致)
判断可能相等(需要把当前的对象给强转之后进行比较)
hashcode 重写规则
根据当前对象的唯一标识进行重写
如果为空就直接返回0,不为空就返回唯一标识的 hashcode 即可。
tostring 重写规则,字符串加上 '' 区别非字符串
类名 + {field1=value1, field2=value2}
例如:person{id=123,name=tom}
例如:
package com.dada.shiro.entity; import java.io.serializable; public class organization implements serializable { private long id;//编号 private string name;// 组织机构名称 private long parentid;// 父编号 private string parentids;// 父编号列表 private boolean available = boolean.false; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public long getparentid() { return parentid; } public void setparentid(long parentid) { this.parentid = parentid; } public string getparentids() { return parentids; } public void setparentids(string parentids) { this.parentids = parentids; } public boolean getavailable() { return available; } public void setavailable(boolean available) { this.available = available; } public boolean isrootnode() { return parentid == 0; } public string makeselfasparentids() { return getparentids() + getid() + "/"; } @override public boolean equals(object that) { // 1.判断一定相等 if(this == that) return true; // 2.判断一定不等 if(that == null || getclass() != that.getclass()) return false; // 3.判断可能相等的情况 organization thatobj = (organization) that; if(id==null ? thatobj.id !=null : !id.equals(thatobj.id)) return false; return true; } @override public int hashcode() { return id == null ? 0 : id.hashcode(); } @override public string tostring() { return "organization {" + "id=" + id + ", name='" + name + "'" + ", parentid=" + parentid + ", parentids='" + parentids + "'" + ", available=" + available + "}"; }}
以上就是equals、hashcode、tostring方法重写的详细内容。
其它类似信息

推荐信息