"Effective Java" learning notes (2)

xiaoxiao2021-03-05  21

This is the 2nd article

two. Methods for all objects

It mainly describes how to correctly rewrite the non-FINAL method in the object class.

Article 7: Please follow the general agreement when rewriting Equals.

Several agreements must be observed by rewriting the Equals method

1. Deflex: x.equals (x) must return true

2. Symmetry: Y.Equals (x) returned must be consistent with x.equals (y)

3. Transmissionability: x.equals (y), Y.Equals (z) If you return to True, then x.equals (z) must also return True

4. Consistency: The value returned multiple times the X.Equals (y) should be consistent

5. Non-air: x.equals (null) must return FALSE

It is most likely to generate errors here that are second and third. To ensure that Article 2 You must ensure that the scope of the object is not expanded. The problems generated by Article 3 are generally in the process of inheritance, and the subclasses expand the parent class, increasing new variables, involving a basic problem for object-oriented relationship theory, that is:

To extend an instantizable class, you must add new features while retaining the Equals convention, without a simple way to do this.

Some prescriptions of high quality Equals methods or say:

a. Use "==" check whether the reference is empty

b. Whether the category of the object using instanceof is correct

c. Convert the real parameters to the correct type and ensure that "key fields" in the actual parameter is matched to the corresponding domain in the current object.

d. After writing Equals, you should check whether it is satisfied: symmetry, delivery, consistency

e. When you rewrite Equals, you have to rewrite Hashcode.

f. Don't try to make the equals method is too smart, add too many equivalents, it will only make things bad

g. Don't make the equals method depend on unreliable resources

h. Do not replace the Object object in the equals declaration to specific type objects

However, you can use a combination instead of inheritance, add an original object in the new class, not inherited it, so you won't encounter the problem that keeps equals.

Article 8: Always change Hashcode when rewriting Equals

If you don't do this, this class cannot work normally with all a collected type based on the hash value, such as Hashset, HashMap. An ideal hash function should distribute uniform distribution of unequal objects in a collection to all possible hash values, and a method provides a way to approach the ideal state.

1. Save a non-0 frequent integer in a INT variable called Result

2. Complete the following steps for each of the key domain f in the object

a. Calculate the column code C:

i. If it is boolean f? 0: 1

II.BYTE, CHAR, ShOR, INT (INT) F

III.long type (int) (f ^ (f >> 32)))

Ivfloat float.floattoinTbits (f)

V. Double Double Double.douBletolongBits (f) Get a LONG type, then follow the III calculation

Vi. If an object reference can be recursively calling hashcode () or calculates another "specification representation"

VII. If it is an array, each element is treated as a separate domain.

b. Calculate the column code result = 37 * Result C by formula.

3. Return to Result

Also, must include the key variables of this class, do not try to achieve performance improvement by excluding critical portions of an object

Article 9: Always rewrite toString ()

This is more in order to provide more valuable information to the user, I rarely use this, embarrassing

Article 10: Carefully rewrite Clone ()

Realize the deep copy of the object. It can be said that the clone () method is another constructor! To prepare a behavior correct clone () method, especially for some of the internal variable domains of some classes. First you have to implement a Cloneable interface, call super.clone () in the Clone () method, and then modify any variables that need to be modified. For example, you have to write a correct Clone method for the Stack of the previous fraud, as follows the following public object clone () throws clonenotsupportedException {

STACK RESULT = (stack) super.clone ();

Result.efficient = (object) Elements.clone ();

Return Result;

}

Prerequisites Elements are not Final.

The rewrite of the clone () method is complex, even dangerous, and another method to provide a copy of the copy object, that is, the copy constructor. This is also not a C proprietary concept.

For example, public example (Example), you passed into a similar type of object, and then the copy of the variable in this constructor is OK. This is a better solution than the complexity of the Cloneable interface.

Article 11: Consider implementing a Compareable interface

The implementation of this interface is similar to the Equals method, but also maintains symmetrical, transmitting, unanimous, etc. Implement this interface

转载请注明原文地址:https://www.9cbs.com/read-38210.html

New Post(0)