One, please observe the Common convention when you overwrite equals
1. You do not need to overwrite the Equals method when any one of the following conditions is true
A, each instance of a class is inherently unique. This is the meaning expressed by the Equals method in object.
b, do not care if the class provides a "logical equality" test function
C. The Equals method covered in the superclass, which is used for subclasses, does not need to be overwritten by itself.
D, the class is private or package-level private, and determines that the Equals method is never called and can not be overwritten. or overwrite the Equals method, with the content throwing an exception.
2. The know-how of the high quality Equals method:
A, use the = = operator to detect whether the parameter is a reference to this object.
B. Use the instanceof operator to check if the parameter is the correct type.
c, convert the parameters to the correct type.
D, for each key field in the class, the detection is equal to the key field in the parameter.
Basic type fields that are not float and double: use = = directly
Reference domain: Recursively calling the Equals method
Float field: Float.compare method
Double field: Double.compare method
E, after completing the Equeal method, ask yourself three questions: Is it symmetrical, transitive, and consistent?
3. Some cautions
1, after covering the Equals method, be sure to overwrite the Hashcode method
2. Do not attempt to make the Equals method too intelligent
3. Do not replace object objects in the equals declaration with other types
4. Note Using @override annotations to ensure coverage
Second, covering the Equals method, always overwrite the Hashcode method three, always overwrite the ToString method four, carefully overwrite the Clone method
1. The Cloneable interface does not contain any methods that determine the behavior of the protected Clone method in object: If a class implements the Cloneable,object clone method, it returns a domain-by-copy of the object, or throw a clonenotsupportedexception exception.
2. All classes that implement the Cloneable interface should overwrite clone with a public method, which first calls Super.clone and then corrects any domain that needs remediation.
3. A good way to provide copies of objects is to provide a copy constructor or copy factory.
V. Consider the implementation of the comparable interface
Effective common methods for all objects in Java learning notes