List of related Reading NotesNo. 7 comply with general conventions when rewriting the equals Method
In the following cases, you do not need to rewrite the equals method: 1. Different instances of the same class are essentially unique, that is, each instance has its own Ontology (identify ). 2. It does not matter whether the class provides the same logic function. 3. The parent class has changed the equals method. For the subclass, the inherited equals method is the most suitable. 4. A class is private or visible to the package, and its equals method is not called. When you need to rewrite the equals method, follow the following conventions: 1. Inversion, that is, X. Equals (X) is true.2. Symmetry, that is, if and only when X. Equals (Y) is true, Y. Equals (X) is also set to true. 3. Transmission, that is, for any X, Y, Z, if X. equals (Y) is true, and Y. equals (z) is also true, then X. equals (z) must also be true.4. Consistency: For any X, Y, if X and Y are not modified, X. equals (y) either returns true consistently or false.5 consistently. For non-null references to X, X. Equals (null) must return false. When rewriting the equals method, we recommend that: 1. Use the = Operator to check whether the real parameter is the same reference to the object. 2. Use instanceof to check whether the real parameters are of the correct type. 3. On the basis of 2, convert the real parameter to the correct type. 4. Check whether the Domain value of the real parameter is equal to that of the current object. 5. After the equals method is compiled, check whether the equivalence relationship is satisfied.
For example:
- Public Boolean equals (Object O)
- {
- If (O = This) return true;
- If (! (O instanceof XXXX) return false;
- XXX in = (XXX) O;
- Return ........
- }
Warning to rewrite the equals method: 1. Do not try to make the equals method do too much. 2. Do not make equals dependent on unreliable resources; otherwise, consistency will be violated. 3. Do not replace the objects in equals with other types.
4. do not provide the public Boolean equals (myclass O) method. This method is a overload and does not overwrite the equals method of the object. Conclusion: Try not to look for trouble without rewriting the equals method. when you really need to rewrite the equals method, follow the general conventions because the object will be continuously transmitted in the program, therefore, the program may run abnormally, or even crash, and it is difficult to find the cause of the program crash. In short, follow the agreement!
No. 8 The hashcode method must be overwritten when the equals method is rewritten.
This must be avoided. Otherwise, an error will occur when you are dealing with the hash-based set. The key issue is that equal hashcode is required for equal objects. For example
If you overwrite the equals method in the phonenumber class but do not overwrite the hashcode method, you will encounter problems when performing the following operations.
- Map M = new hashmap ();
- M. Put (New phonenumber (408,863,333 4), "Ming ")
When
You call M. Get (New
In phonenumber (408,863,333 4), you want to get Ming but you get null. Why is there two
For phonenumber instances, one is put and the other is get, but the two instances with the same logic get different hashcode. How can we get the previously stored Ming
And.
No. 9 always needs to rewrite the tostring () method
The format returned by the tostring method of the object is the class type plus @ and the hexadecimal hashcode, which is hard to understand. It is best to provide the tostring method in your class to better express the instance information. Otherwise, how can someone else understand it.
In practical applications, the tostring method should return all interesting information contained in the object. At the same time, it is best to provide a matched constructor or static factory method in the program, so that programmers can convert the object and its string representation back and forth.
When implementing the tostring method, you must determine whether to specify the format of the returned value in the document. The specified format can be used as a standard, unambiguous expression, but it also embeds the string representation into permanent data. If the expression is changed in the future, it will affect the system code and data. Whether or not you decide to specify the format, you should clearly express your intention in the code. You can provide a programming access path for the information contained in the return value of tostring in the class to obtain the information returned by the tostring method, so as to avoid errors caused by the programmer's own parsing of the string. No. 10 rewrite clone with caution (for details about the clone method, see the Java clone method). If an object is to be cloned, the clone () interface must be implemented. This interface does not define any method, however, if you do not implement this interface, call the clone method
Clonenotsupportedexception occurs, which is the type of the interface called Mixin by the author. The clone () method can overwritePublic object clone () <br/>{< br/> try <br/>{< br/> return Super. clone (); <br/>}< br/> catch (clonenotsupportedexception e) <br/>{}< br/>}However, when the class you want to clone contains modifiable reference fields, you must copy the entire class blueprint, if you modify the cloned object and the original Instance is affected, this is not desirable. So clone ()
Public object clone () throws clonenotsupportedexception <br/>{< br/> stack result = (stack) super. clone (); <br/> result. elements = (object []) elements. clone (); <br/> return result; <br/>}Elements is a reference field that can be modified in the stack class. Note that if elements is final, we can't do anything, because we can't assign a new value to it. in fact, if it is not necessary, it is best not to use it. If the clone method is improperly implemented, it will bring hidden bugs to the system, if you want to use similar functions, the best way is to provide some other methods (copy constructors or provide a static factory method to replace constructors) to replace object copying, or simply do not provide such capabilities. Cloneable has many problems, so it is safe to say that other interfaces should not be extended (extend), and classes designed for inheritance should not be implemented (implement. No. 11 The compareto method for implementing the comparable interface is Java. lang. the only method in the comparable interface. It allows simple equal comparison and sequential comparison. If a class implements the comparable interface, it indicates that its instance has a built-in sorting relationship. All value classes in the Java platform library implement comparable. When the current object is compared with the specified object in sequence, a negative integer, 0, or a positive integer (<, =,>) is returned. If the type of the specified object cannot be compared, the classcastexception or nullpointexception exception is thrown. The compareto method must comply with the following constraints: Self-inversion, symmetry, transmission, and non-null constraints. When the compareto Method for numerical comparison is implemented, it also prevents value overflow.