<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Scott@JAVA

    Java, 一杯濃濃的咖啡伴你到深夜

    《Effective Java》Chapter 3

    Chapter 3. Methods Common to All Objects

    Item 7: Obey the general contract when overriding equals
    This is the right thing to do if any of the following conditions apply:
       a) Each instance of the class is inherently unique.
       b) You don't care whether the class provides a "logical equality" test.
       c) A superclass has already overridden equals, and the behaviour inherited from the superclass is appropriate for this class.
       d) The class is private or package-private, and you are certain that its equals method will never be invoked.

    Here's a recipe for a high-quality equals method:
       1) Use the == operator to check if the argument is a reference to this object.
       2) Use the instanceof operator to check if the argument is of the correct type.
       3) Cast the argument to the correct type.
       4) For each "significant" field in the class, check to see if that field of the argument matches the corresponding field of this object.
       5) When you are finished writing your equals method, ask yourself three questions: Is it symmetric, is it transitive, and is it consistent?

    Here are a few final caveats:
       a) Always override hashCode when you override equals.
       b) Don't try to be too clever.
       c) Don't write an equals method that relies on unreliable resources.
       d) Don't substitute another type for Object in the equals declaration.

    Item 8: Always override hashCode when you override equals
    You must override hashCode in every class that overrides equals.
    The key provision that is violated when you fail to override hashCode is the second one:
    Equal objects must have equal hash codes.

    Here is a simple recipe:
    1. Store some constant nonzero value, say 17, in an int variable called result.
    2. For each significant field f in your object (each field taken into account by the equals method, that is), do the following:
       a. Compute an int hash code c for the field:
          i. If the field is a boolean, compute (f ? 0 : 1).
          ii. If the field is a byte, char, short, or int, compute (int)f.
          iii. If the field is a long, compute (int)(f ^ (f >>> 32)).
          iv. If the field is a float compute Float.floatToIntBits(f).
          v. If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step 2.a.iii.
          vi. If the field is an object reference and this class's equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a "canonical representation" for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0 (or some other constant, but 0 is traditional).
          vii. If the field is an array, treat it as if each element were a separate field. That is, compute a hash code for each significant element by applying these rules recursively, and combine these values as described in step 2.b.
       b. Combine the hash code c computed in step a into result as follows: result = 37*result + c;
    3. Return result.
    4. When you are done writing the hashCode method, ask yourself whether equal
    instances have equal hash codes. If not, figure out why and fix the problem

    Do not be tempted to exclude significant parts of an object from the hash code computation to improve performance.

    Item 9: Always override toString
    Providing a good toString implementation makes your class much more pleasant to use.
    When practical, the toString method should return all of the interesting information contained in the object.
    Whether or not you decide to specify the format, you should clearly document your intentions.
    It is always a good idea to provide programmatic access to all of the information contained in the value returned by toString.

    Item 10: Override clone judiciously
    If you override the clone method in a nonfinal class, you should return an object obtained by invoking super.clone.
    In practice, a class the implements Cloneable is expected to provide a properly functioning public clone method.
    public Object clone() {
        
    try {
            
    return super.clone();
        }
     catch(CloneNotSupportedException e) {
            
    throw new Error("Assertion failure"); // Can't happen
        }

    }

    If, however, your object contains fields that refer to mutable objects, using this clone
    implementation can be disastrous. Neglect the rest, refer to the book for more details...

    Item 11: Consider implementing Comparable
    The compareTo method is not declared in Object. Rather, it is the sole method in the java.lang.Comparable interface.
    Array element comparisons of Chapter 11: Collections of Objects in <<Thinking in Java>> 3rd ed. Revision 4.0, has a more practical way to explain this. 

    posted on 2005-12-18 00:23 Scott@JAVA 閱讀(448) 評論(0)  編輯  收藏 所屬分類: Effective Java

    主站蜘蛛池模板: 亚洲天堂免费在线| 国产精品手机在线亚洲| 亚洲国产第一站精品蜜芽| 亚洲国产精品VA在线看黑人 | 免费v片在线观看品善网| 亚洲日韩人妻第一页| 亚洲国产精品高清久久久| 国产亚洲免费的视频看| 成人毛片免费观看视频大全| 免费人成在线观看网站品爱网日本| 亚洲欧美日韩久久精品| 99在线热播精品免费99热| 无码国产精品久久一区免费 | 国产AV无码专区亚洲精品| 中文字幕乱理片免费完整的| 99re热免费精品视频观看| 亚洲熟妇无码av另类vr影视| 久久伊人免费视频| gogo全球高清大胆亚洲| 亚洲日本乱码一区二区在线二产线| 七次郎成人免费线路视频| 99视频在线精品免费观看6| 久久久亚洲欧洲日产国码二区| 久久精品国产亚洲AV未满十八| 18禁无遮挡无码国产免费网站| 亚洲国产婷婷香蕉久久久久久| xxx毛茸茸的亚洲| 亚洲香蕉免费有线视频| 在线观看亚洲AV日韩AV| 亚洲黄片手机免费观看| 久久永久免费人妻精品下载 | 777亚洲精品乱码久久久久久 | 亚洲国产精品专区| 无码人妻丰满熟妇区免费| 亚洲综合熟女久久久30p| 精品免费AV一区二区三区| 84pao国产成视频免费播放| 国产精品亚洲а∨无码播放| 国产日韩精品无码区免费专区国产| 无码欧精品亚洲日韩一区夜夜嗨| 黄色网站软件app在线观看免费 |