覆盖equals【引自http://www.javapractices.com/…
2008-02-23 09:41:17来源:互联网 阅读 ()
Implementing equalsDiscussion:All objects have both identity (the object's location in memory) and state (the object's data). The == operator always compares identity. The default implementation of equals compares identity as well.
Sometimes the default implementation of equals has the desired behaviour (as in a type-safe enumeration, for example), but equals should usually compare state, not identity. This is particularly true for "data-centric" classes which map to database records.
hashCode and equals are closely related :
- if you override equals, you must override hashCode
- hashCode must generate equal values for equal objects
If you extend a concrete class, and add a new field which contributes to equals, then it is not possible to write a perfectly correct equals method for the new class. Instead, you should use composition instead of inheritance.
Example
Here is an implementation of equals for a data-centric class. It demonstrates how different types of fields are treated:
- object fields, including collections : use equals
- type-safe enumerations : use either equals or == (they amount to the same thing, in this case)
- possibly-null object fields : use both == and equals
- array fields : use Arrays.equals
- primitive fields other than float or double : use ==
- float : convert to int using Float.floatToIntBits, then use ==
- double : convert to long using Double.doubleToLongBits, then use ==
The above policies can be collected in a utility class :
import java.util.Arrays;
/**
* Collected methods which allow easy implementation of <code>equals</code>.
*
* Example use case in a class called Car:
* <pre>
public boolean equals(Object that){
if ( this == that ) return true;
if ( !(that instanceof Car) ) return false;
Car thatCar = (Car)that;
return
EqualsUtil.areEqual(this.fName, that.fName) &&
EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors) &&
EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage) &&
EqualsUtil.areEqual(this.fColor, that.fColor) &&
Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks); //array!
}
* </pre>
*
* <em>Arrays are not handled by this class</em>.
* This is because the <code>Arrays.equals</code> methods should be used for
* array fields.
*/
public final class EqualsUtil {
static public boolean areEqual(boolean aThis, boolean aThat){
//System.out.println("boolean");
return aThis == aThat;
}
static public boolean areEqual(char aThis, char aThat){
//System.out.println("char");
return aThis == aThat;
}
static public boolean areEqual(long aThis, long aThat){
/*
* Implementation Note
* Note that byte, short, and int are handled by this method, through
* implicit conversion.
*/
//System.out.println("long");
return aThis == aThat;
}
static public
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Hibernate 一对一例
下一篇:Eclipse启动参数大全
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
