[英]regarding using this in implementing equals for comparing objects in Java
我從該論壇的另一個問題線程中看到了有關定義equals
的代碼段。 但是我的問題是以下代碼做什么,為什么需要它? 謝謝。
if (obj == this)
{
return true;
}
原始代碼如下所示。 http://stackoverflow.com/questions/8338326/what-does-equalsobject-obj-do#
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (obj == null)
{
return false;
}
if (obj instanceof Contact)
{
Contact other = (Contact)obj;
return other.getFirstName().equals(getFirstName()) &&
other.getLastName().equals(getLastName()) &&
other.getHomePhone().equals(getHomePhone()) &&
other.getCellPhone().equals(getCellPhone());
}
else
{
return false;
}
}
實現Java類的equals
方法是一個經常討論的話題。 通常,您需要確保在以下情況下該方法返回true
:
this
進行比較的對象是關於指針的相同對象(對象==此)或 this
進行比較的對象關於您的域是相同的 所以,關於你的問題,你問前者的情況下,其中的Java剛剛檢查,如果object
通過, this
是指向內存中(也許這個鏈接可以幫助你在同一地址http://www.javaworld.com/article/2072762 /java-app-dev/object-equality.html )。
簡短說明:實現equals
方法時,通常遵循以下模式:
檢查您是否具有相同的對象(關於指針),即
if (object == this) return true;
您要確保沒有任何null
實例(為避免進一步的NullPointerException
,如果它為null
則它永遠不能相等,即,
else if (object == null) return false;
您檢查對象在域中是否相等,是否相等(例如,如果id
相等,則可以假定dao相等); 在進行特定於域的驗證之前,通常請務必確保您擁有正確實例的對象,即
else if (this.getClass().equals(obj.getClass()) { ... }
要么
else if (this.getClass().isInstance(obj.getClass()) { ... }
在其他所有情況下,您都想返回false,即
else return false;
注意:在實現equals
方法時,通常使用Objects.equals(...)
方法來比較實例的不同屬性。
例:
@Override
public boolean equals(final Object obj) {
if (obj == this) {
// we have the same object regarding the pointers (e.g., this.equals(this)), or have a look at 'Manjunath M' post for a different example
return true;
} else if (obj == null) {
// we compare with null (e.g., this.equals(null))
return false;
} else if (Contact.class.isInstance(obj.getClass())) {
/*
* If you want to be more strict you could also use:
* getClass().equals(obj.getClass())
*/
Contact other = Contact.class.cast(obj);
return Objects.equals(other.getFirstName(), getFirstName()) &&
Objects.equals(other.getLastName(), getLastName()) &&
Objects.equals(other.getHomePhone(), getHomePhone()) &&
Objects.equals(other.getCellPhone(), getCellPhone());
} else {
return false;
}
}
Java中的'=='用於比較對象的引用。 如果將對象與自己的自身進行比較,則equals函數必須返回true。 這就是為什么將此條件添加到equals方法中的原因。
對於像int,char'=='這樣的原始數據類型,它比較值,對於對象,它比較那里的引用。 一些文獻說,'=='的工作確實很奇怪,因為比較引用並不是很重要。
這是指if語句所在的同一對象。 通過比較obj ==,它檢查傳遞的對象是否是內部調用equals函數的對象本身。
您也可以檢查這個問題: Java中“ this”的含義是什么?
如果您沒有在各自的類中覆蓋equals方法,它將執行已經在Object類(所有類的最高級類)的equals方法中編寫的實現,在那里,他們正在檢查這種相等性
obj == obj1
上面提到的代碼實際上是根據創建兩個對象的內存位置地址檢查是否相等,因此,上面的代碼行檢查我們嘗試檢查相等性的兩個對象是否具有相同的引用。 如果兩者都具有相同的引用,則我們無需再做任何雜技檢查相等性。 像這樣
Car car = new Car();
Car car1 = car;
car.equals(car1);
您可以清楚地看到car和car1具有相同的引用方式,它們都是具有相同存儲地址的相同對象。 因此不必通過檢查屬性來檢查相等性,它只是避免執行額外的代碼。
this
是指在其下執行equals
函數的類的對象。 如果obj
是為了進行比較,以平等,需要其他物體obj
和this
指的是相同的實例,然后他們是真的吧?
例如
MyClass myObject = new MyClass();
myObject.equals(obj);
在這種情況下,equals方法中的this
和obj
都將引用同一實例myObject
。 在這種情況下,我們不需要檢查這些對象的其他屬性是否相等。
TL; DR-檢查這種情況有助於優化,因為這避免了檢查兩個對象是否相等所需的額外工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.