覆盖equals【引自http://www.javapractices.com/…

2008-02-23 09:41:17来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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
Objects placed in a List , Set, or Map (as either a key or value) should have an appropriate definition of equals. (See, for example, the Javadoc for Collection.contains , Map.containsKey, and Map.containsValue .)

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 ==
It is worth noting that if fields are implemented with wrapper classes (Integer, Boolean, and so on), then implementation of equals is simpler, since there is only one case : calling the equals method recursively. (The compareTo method is also simplified in this case.)

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启动参数大全