About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期日, 6月 22, 2008

Effective Java筆記 - Chapter 3


宣告 @Override 確定有正確Override

1. equals
    - Reflexive
    - Symmetric
    - Transitive
    - Consistent
    - not equals to null
    - 使用composition, 不用extends
    - 先測試 instance of
    - 不要依靠unreliable resources
    - 測試最顯著的fields

2. Always override hashCode when overriding equals
    - same object return the same hash code  
    - equal object must have equal hash codes
    - two different object return different hash code

3. Aways override toString

4. Override clone judiciously
    - implement Clonable
    - 如果只有 primitive fields, 可直接呼叫 super.clone()
    - 物件則只會clone reference pointer, 需特別處理
    - 不可以有finalize object
    - 需留意clone時的performance
    - 提供另外的copy constructor
    - 盡量不要用

5. Consider implementing Comparable
    - 與equals類似的性質

0 意見: