override the equals & hashcode & compareTo & toString

本文介绍了如何正确地重写equals、hashCode、compareTo和toString方法,使用了org.apache.commons.lang.builder包中的工具类来简化实现过程,并确保这些方法的一致性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结一下相关的 equals/ hashCode / compareTo / toString method写法
1. org.apache.commons.lang.builder.EqualsBuilder
注意:复写equals,必须要复写hashCode, 在equals比较的Field必须也出现在hashCode里,反之亦然。
@Override
public boolean equals(Object o) {
if (o == null) { return false; }
if (! (o instanceof AccountInstrument)) {
return false;
}
if (o == this) { return true; }
AccountInstrument rhs = (AccountInstrument) o;
return new EqualsBuilder()
.append(accountId, rhs.accountId)
.append(instrumentId, rhs.instrumentId)
.isEquals();

}
2. org.apache.commons.lang.builder.HashCodeBuilder
请参考equals, 可以使用HashCodeBuilder(17, 35) as HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber)选择任意、非零、奇数
// cache for toString() and hashCode() as this is a immutable object
private Integer hashCodeCache = null;
private String toStringCache = null;
@Override
public int hashCode() {
if (hashCodeCache == null) {
hashCodeCache = new HashCodeBuilder()
.append(accountId).append(instrumentId).toHashCode();
}
return hashCodeCache;
}

3. org.apache.commons.lang.builder.ToStringBuilder
ToStringStyle定义输出的格式,可以创建子类实现新的输出格式。
@Override
public String toString() {
if (toStringCache == null) {
toStringCache = ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
}
return toStringCache;
}
在API文档的例子:
public class MyStyle extends ToStringStyle {
protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
if (value instanceof Date) {
value = new SimpleDateFormat("yyyy-MM-dd").format(value);
}
buffer.append(value);
}
}


4. org.apache.commons.lang.builder.CompareToBuilder
public int compareTo(AccountInstrument o) {
return new CompareToBuilder()
.append(this.accountId, o.accountId)
.append(this.instrumentId, o.instrumentId)
.toComparison();
}
好的,我明白了。下面是题目所要求的代码,您可以参考一下: ```java import java.util.Date; public class Student implements Comparable<Student> { private String name; private Date birthday; private int id; private String school; public Student(String name, Date birthday, int id, String school) { this.name = name; this.birthday = birthday; this.id = id; this.school = school; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", birthday=" + birthday + ", id=" + id + ", school='" + school + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; Student student = (Student) o; if (id != student.id) return false; if (!name.equals(student.name)) return false; if (!birthday.equals(student.birthday)) return false; return school.equals(student.school); } @Override public int hashCode() { int result = name.hashCode(); result = 31 * result + birthday.hashCode(); result = 31 * result + id; result = 31 * result + school.hashCode(); return result; } @Override public int compareTo(Student o) { return this.id - o.id; } } ``` 这是一个完整的符合要求的代码,其中包括了定义了一个学生类,成员变量包括:姓名,生日,学号,学校;重写了方法toStringequalshashCode;实现了接口Comparable,按照学号大小进行比较;定义构造方法。如果您对其中的实现细节不太理解,可以在回复中向我提问,我会尽力给您解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值