提问人:bodn19888 提问时间:9/28/2020 最后编辑:Mureinikbodn19888 更新时间:10/7/2020 访问量:1301
使用 equals 方法在 Java 中测试对象的相等性
Testing equality of objects in Java with equals method
问:
我想问一个关于在 Java 中使用 equals 方法测试对象相等性的问题。
我是 Java 的初学者,目前正在阅读 Dummies Java 9-in-1 书籍。
我编写了以下代码来检查两个对象的相等性:Employee
public class TestEquality2 {
public static void main (String [] args) {
Employee emp1 = new Employee ("Martinez", "Anthony");
Employee emp2 = new Employee ("Martinez", "Anthony");
if (emp1.equals(emp2))
System.out.println("These employees are the same");
else
System.out.println("These employees are different.");
}
}
class Employee {
private String firstName;
private String lastName;
public Employee (String firstName, String lastName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
public boolean equals (Object obj) {
// an object must equal itself
if (this == obj)
return true;
// no object equals null
if (this == null)
return false;
if (this.getClass() != obj.getClass())
return false;
// Cast to an employee, then compare the fields
Employee emp = (Employee) obj;
// is this the string's equals method?
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
}
}
关注线是该方法的最后一条。equals(Object obj)
根据代码,我已经覆盖了默认方法并提供了我自己的设计,但我在这里感到困惑:Object equals ()
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
我知道这是一个字符串,但是我在这里使用的方法,这是 a 的方法还是我刚刚定义的方法?如果是后者,我知道我会创建一个递归情况,尽管我有信心我正在使用 但我想澄清完成。lastName
equals()
equals()
String
String equals()
答:
lastName
是一个 ,所以调用将使用 的方法的实现。当然,名字的比较也是如此。String
this.lastName.equals(emp.getLastName())
String
equals
基本上,你覆盖了 equals() 方法,因为你希望在内存中有两个不同的对象在你的应用程序中被认为是相等的,就像你创建了两个不同的 Employee 类对象一样,如下所示:
Employee emp1 = new Employee ("Martinez", "Anthony");
Employee emp2 = new Employee ("Martinez", "Anthony");
这些对象实际上是内存中的两个不同对象,或者从现实生活中的角度来看,它们是两个不同的个体,如果您不覆盖 equals() ,它将从 Object 类中检查 equals(),该类具有以下实现:
public boolean equals(Object obj) {
return (this == obj);
}
emp1.equals(emp2) 将返回 false。
但可能是你的应用程序逻辑是这样的,你希望这两个人被认为是相等的(相同的),所以在这种情况下,你将覆盖equals()方法。
在覆盖相等时,您可以选择希望两个个人(员工或对象)相等的基础,例如在您的情况下,您选择了 firstName 和 lastName(您也可以选择唯一的 lastName,具体取决于您的要求)。
所以在下面的一行中,基本上你是说,如果两个员工 firstName 和 surName 相等,那么即使你知道他们是两个不同的个体(内存中的对象):
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
为此,您正在 lastName 和 firstName 上调用 String 类的 equals(),它们是 Strings.String 类,Java 库开发人员已经覆盖了 equals() 方法,如下所示:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
希望这对您有所帮助。
上一个:C 语言中的运算符优先级和关联性
评论
if (this == null)
if (obj == null)
non-null rule
if(this==null) return true
if(obj==null) return true
false
,而不是 。if(this==null)
NullPointerException
if(obj==null)
true