private String name;
private int age;
private Bar bar;
public Foo(String name, int age) {
this.name = name;
this.age = age;
}
//省略getter/setter方法
}
private String name;
public Bar(String name) {
this.name = name;
}
//省略getter/setter方法
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(name)
.append(age)
.append(bar)
.toHashCode();
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public boolean equals(Object obj) {
boolean flag = false;
if (obj != null && Foo.class.isAssignableFrom(obj.getClass())) {
Foo f = (Foo) obj;
flag = new EqualsBuilder()
.append(name, f.getName())
.append(age, f.getAge())
.append(bar, f.getBar()).isEquals();
}
return flag;
}
return CompareToBuilder.reflectionCompare(this, obj);
}
int flag = -1;
if (obj != null && Foo.class.isAssignableFrom(obj.getClass())) {
Foo f = (Foo) obj;
flag = new CompareToBuilder()
.append(name, getName())
.append(age, f.getAge())
.append(bar, f.getBar())
.toComparison();
}
return flag;
}