EQ_SELF_USE_OBJECT 错误
EQ_SELF_USE_OBJECT Bug
我正在编写一个 equals 方法,其中如果两个名字具有相同的名字、中间名和姓氏,则它们被视为相等。但是,我不断收到错误消息
"This class defines a covariant version of the equals() method, but
inherits the normal equals(Object) method defined in the base
java.lang.Object class. The class should probably define a boolean
equals(Object) method."
当我将其更改为 Object other 作为参数时,出现 "no such method" 错误。
public boolean equals(Name other) {
boolean sameFirstName = firstName.equals(other.firstName);
boolean sameMiddleName = middleName.equals(other.middleName);
boolean sameLastName = lastName.equals(other.lastName);
if (sameFirstName && sameMiddleName && sameLastName) {
return true;
}
return false;
}
您通过使用不同的参数来键入 Object
来重载方法 equals。以下应该适合您。
@Override
public boolean equals(Object object) {
if(this == object) return true;
if(!(object instanceof Name)) return false;
Name other = (Name) object;
boolean sameFirstName = firstName.equals(other.firstName);
boolean sameMiddleName = middleName.equals(other.middleName);
boolean sameLastName = lastName.equals(other.lastName);
if (sameFirstName && sameMiddleName && sameLastName) {
return true;
}
return false;
}
您必须为调用 'other' 的参数使用对象类型。然后进行检查和铸造实例。请参考这个answer
如果您使用Java 7或更高版本,您可以使用此代码:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(firstName, person.firstName) &&
Objects.equals(middleName, person.middleName) &&
Objects.equals(lastName, person.lastName);
}
并且在IDE中有一个自动生成的等号:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return !(firstName != null ? !firstName.equals(person.firstName) : person.firstName != null)
&& !(middleName != null ? !middleName.equals(person.middleName) : person.middleName != null)
&& !(lastName != null ? !lastName.equals(person.lastName) : person.lastName != null);
}
请注意,还应该进行 NPE 检查。
我正在编写一个 equals 方法,其中如果两个名字具有相同的名字、中间名和姓氏,则它们被视为相等。但是,我不断收到错误消息
"This class defines a covariant version of the equals() method, but inherits the normal equals(Object) method defined in the base java.lang.Object class. The class should probably define a boolean equals(Object) method."
当我将其更改为 Object other 作为参数时,出现 "no such method" 错误。
public boolean equals(Name other) {
boolean sameFirstName = firstName.equals(other.firstName);
boolean sameMiddleName = middleName.equals(other.middleName);
boolean sameLastName = lastName.equals(other.lastName);
if (sameFirstName && sameMiddleName && sameLastName) {
return true;
}
return false;
}
您通过使用不同的参数来键入 Object
来重载方法 equals。以下应该适合您。
@Override
public boolean equals(Object object) {
if(this == object) return true;
if(!(object instanceof Name)) return false;
Name other = (Name) object;
boolean sameFirstName = firstName.equals(other.firstName);
boolean sameMiddleName = middleName.equals(other.middleName);
boolean sameLastName = lastName.equals(other.lastName);
if (sameFirstName && sameMiddleName && sameLastName) {
return true;
}
return false;
}
您必须为调用 'other' 的参数使用对象类型。然后进行检查和铸造实例。请参考这个answer
如果您使用Java 7或更高版本,您可以使用此代码:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(firstName, person.firstName) &&
Objects.equals(middleName, person.middleName) &&
Objects.equals(lastName, person.lastName);
}
并且在IDE中有一个自动生成的等号:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return !(firstName != null ? !firstName.equals(person.firstName) : person.firstName != null)
&& !(middleName != null ? !middleName.equals(person.middleName) : person.middleName != null)
&& !(lastName != null ? !lastName.equals(person.lastName) : person.lastName != null);
}
请注意,还应该进行 NPE 检查。