1. A value class is simply a class that represents a value, such as Integer or Date. A programmer who compares references to value objects using the equals method expects to find out whether they are logically equivalent, not whether they refer to the same object.
2. Not to override the equals method is the right thing to do if any of the following conditions apply:
a) Each instance of the class is inherently unique. (Thread)
b) You don’t care whether the class provides a “logical equality” test.(Random)
c) A superclass has already overridden equals, and the superclass behavior is appropriate for this class.(AbstractSet, AbstractList, AbstractMap)
d) The class is private or package-private, and you are certain that its equals method will never be invoked.
e) A class that uses instance control to ensure that at most one object exists with each value.(Enum)
3. When you override the equals method, you must adhere to its general contract:
a) Reflexive: For any non-null reference value x, x.equals(x) must return true.
b) Symmetric: For any non-null reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true.
c) Transitive: For any non-null reference values x, y, z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
d) Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
e) For any non-null reference value x, x.equals(null) must return false.
4. Not only is overriding the equals method necessary to satisfy programmer expectations; it enables instances to serve as map keys or set elements with predictable, desirable behavior.
5. There is no way to extend an instantiable class and add a value component while preserving the equals contract. Preserving the equals contract by using a getClass test in place of the instanceof test in the equals method will break Liskov substitution principle.( superclass instance cannot compare with subclass instance) Note that you can add a value component to a subclass of an abstract class without violating the equals contract because it is impossible to create a superclass instance directly.
6. java.sql.Timestamp extends java.util.Date and adds a nanoseconds field. The equals implementation for Timestamp does violate symmetry and can cause erratic behavior if Timestamp and Date objects are used in the same collection or are otherwise intermixed.
7. Do not write an equals method that depends on unreliable resources. equals methods should perform deterministic computations on memory-resident objects.
8. java.net.URL’s equals method relies on comparison of the IP addresses of the hosts associated with the URLs. Translating a host name to an IP address can require network access, and it isn’t guaranteed to yield the same results over time. This can cause the URL equals method to violate the Consistency contract.
9. A recipe for a high-quality equals method:
a) Use the == operator to check if the argument is a reference to this object for performance optimization.
b) Use the instanceof operator to check if the argument has the correct type. (Use an interface if the class implements an interface that refines the equals contract to permit comparisons across classes that implement the interface. Such as Set, List, Map, and Map.Entry.
c) Cast the argument to the correct type.
d) For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object. (If the type in step b) is an interface, you must access the argument’s fields via interface methods; )
e) When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?
10. For primitive fields whose type is not float or double, use the == operator for comparisons; for object reference fields, invoke the equals method recursively; for float fields, use the Float.compare method; and for double fields, use Double.compare.(due to the Float.NaN and -0.0f exception)
11. To avoid the possibility of a NullPointerException, use this idiom to compare such fields:
(field == null ? o.field == null : field.equals(o.field))
This alternative may be faster if field and o.field are often identical:
(field == o.field || (field != null && field.equals(o.field)))
12. For best performance, you should first compare fields that are more likely to differ, less expensive to compare, or, ideally, both. If a redundant field amounts to a summary description of the entire object, comparing this field will save you the expense of comparing the actual data if the comparison fails.
13. The File class shouldn’t attempt to equate symbolic links referring to the same file. Thankfully, it doesn’t.
14. Don’t substitute another type for Object in the equals declaration. The problem is that this method does not override Object.equals, whose argument is of type Object, but overloads it instead.
相关推荐
Item 10: Obey the general contract when overriding equals Item 11: Always override hashCode when you override equals Item 12: Always override toString Item 13: Override clone judiciously Item 14: ...
- 让水龙头的水一直流:leave the tap running - 保持安静:keep quiet - 摘花:pick flowers - 遵守交通规则:obey traffic rules - 排队等候:queue up 2. 单词填空: - 举止:behavior - 摘:pick - ...
- 遵守:obey - 灯:light - 保持:keep - 穿过,穿行:cross - 人行道:pavement/sidewalk - 容易的:easy - 容易地:easily - 规则:rule - 斑马线:zebra crossing 2. 短语理解: - 寻找:look for -...
"Obey"是一个针对这些问题的开源库,它提供了一种可扩展的解决方案,专为异步数据建模和校验而设计。这个框架允许开发者高效地创建复杂的模型定义,并进行实时的数据验证,尤其适用于构建大型的、数据驱动的前端应用...
Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, and JavaScript by Harry J. W. Percival English | 2 Aug. 2017 | ISBN: 1491958707 | ASIN: B074HXXXLS | 604 Pages | AZW...
- 守法:observe/obey the law - 遵守交规:observe traffic regulations - 改进社会风气:improve public morals - 违法:offend against the law - 侵犯个人隐私:invasion of privacy - 违反公共规章:...
2. **动词短语cut in (on sb./sth.)**:这个短语有“打断(谈话)、插嘴”的含义,例如:"Jerry cut in on his sister when she wanted to tell the truth."(杰瑞在他姐姐想说实话时打断了她。)此外,它还可以表示...
4. **abideby = obey; complywith, conformto** - **含义**: 遵守 - **例句**: All members must abide by the rules of the club. 5. **abnormalities** - **含义**: 异常、不正常的事物 - **例句**: The ...
随机微分系统的指数稳定性研究,尤其是在系统系数满足多项式增长条件下的非线性随机微分系统,是一个在现代控制理论中至关重要的课题。本文由刘磊、沈轶和苏鹏共同撰写,针对系数具有多项式增长的随机微分系统,探讨...
- "服从"用obey,例如:Drivers must obey traffic rules for safety. - "罚款单;票"是ticket,如:If you break the law, you may receive a ticket. 9. 其他词汇: - "东西;事情"是thing,例如:What's the ...
- **例句**:Every citizen has the obligation to pay taxes and obey the law. #### 2.4 Subordinate - **含义**:次要的,从属的,下级的;下属;服从。 - **例句**:In the military hierarchy, privates are ...
- 遵守规章制度:obey the rules and regulations - 对…来说很难:be hard for... - 一开始:at first - 尽他们最大努力去做某事:try their best to do something - 变得越来越好:get better and better - ...
可以通过NPM安装Obey: npm install obey --save API文档 提供了详细的API文档,以帮助您使用,修改或贡献Obey库。 规则 规则是应如何验证值的核心定义: const obey = require ( 'obey' ) const firstName = ...
- 遵守规则:obey the rules - 在家里有很多的规则:There are many rules at home. - 不要把脏盘子留在厨房里:Don't leave dirty dishes in the kitchen. - 我在能看电视之前必须要读书:I have to read ...
- 制定/遵守规章制度:make/obey rules - 许多规定:many rules - 太多规定:too many rules - 上课/上学迟到:be late for class/school - 在走廊里:in the corridor - 在餐厅就餐:eat in the canteen - ...
这个词组表示服从规则、承诺或决定,例如:“We must abide by the contract we signed.”(我们必须遵守我们签署的合同。) 2. be absent from…:缺席,不在。用于描述某人未出席某场合或未完成某活动,如:“She...
- **例句**:Everyone must abide by the terms of the contract. - **stick to**:坚持。 - **例句**:He sticks to his principles no matter what. #### 134. be confronted with (面对) - **释义**:不得不...
- We must all abide by the rules set out in the contract.(我们都必须遵守合同中规定的所有规则。) ### 2. be absent from - **含义**:表示某人不在某个地方或场合。 - **用法**:后面接地点或活动名称。 - ...
拓展:break the rules(违反规定),obey the rules(遵守规定),be for(支持,赞成)。 - **短语 in trouble**:表示“遇到麻烦”或“处于困境”。如:“遇上麻烦”可以用 "in great trouble" 强调程度。拓展:...
5. 交通安全:obey the traffic rules, no jaywalking. 总的来说,这份试题全面地测试了学生的英语综合能力,并通过阅读材料提醒学生养成良好的生活习惯。在英语学习中,不仅要注重词汇和语法的学习,还要培养阅读...