equals,hashcode,toString

今天看到一个关于equals,hashcode,toString方法的类型下面是关于具体实现

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

//使用表示运行时才知道类型
DBObject dbObject = (DBObject) o;

//在if()中使用'?:',!equal和!=配套使代码简洁
if (alias != null ? !alias.equals(dbObject.alias)
: dbObject.alias != null)
return false;
if (expression != null ? !expression.equals(dbObject.expression)
: dbObject.expression != null)
return false;

return true;
}


@Override
public int hashCode() {
int result = alias != null ? alias.hashCode() : 0;
result = 31 * result + (expression != null ? expression.hashCode() : 0);
return result;
}


@Override
public String toString() {
StringBuilder sb = new StringBuilder(expression);
if (hasAlias()) {
sb.append(SPACE).append(AS).append(SPACE).append(alias);
}
return sb.toString();
}

你可能感兴趣的:(equals,hashcode,toString)