Java: howto write equals() short
2022-09-05 00:39:38
当我不得不写近10行代码说.您可以轻松地看到,在这种书写方式中,行数会随着属性数的增加而急剧增加。2 Objects are equal, when their type is equal and both's attribute is equal
public class Id implements Node {
private String name;
public Id(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == null)
return false;
if (null == (Id) o)
return false;
Id i = (Id) o;
if ((this.name != null && i.name == null) || (this.name == null && i.name != null))
return false;
return (this.name == null && i.name == null) || this.name.equals(i.name);
}
}