Va problem: this problem occurs when I write a snake program today.
Public class coordinate {
Public int headx, heady;
Coordinate (INT headx, int heady ){
This. headx = headx;
This. Heady = heady;
}
Public Boolean equals (coordinate c ){
If (this. headx = C. headx & this. Heady = C. Heady ){
Return true;
} Else {
Return false;
}
}
Note: coordinate is a coordinate class. The reason for rewriting the equals method is to compare whether the snake body has reached the recorded turning point of the Snake Head. Therefore, the equals method is rewritten, but no effect is found, returns false even if the values of X and Y are the same.
To get familiar with the equals method, I first wrote the following verification program:
Public class testequals {
Public static void main (string [] ARGs ){
Dog d1 = new dog (1 );
Dog D2 = new dog (1 );
System. Out. println (d1 = d2 );
System. Out. println (d1.equals (D2 ));
}
}
Class dog {
Int weight;
Dog (INT weight ){
This. Weight = weight;
}
Public Boolean equals (DOG d ){
If (this. Weight = D. Weight ){
Return true;
} Else {
Return false;
}
}
}
Of course, the results are the same. For further verification, the hashcode () method is further Rewritten:
Public int hashcode (){
Return 0;
}
Because sometimes hashcode is preferred. However, the results still do not work. I had to retrieve the original video tutorial from Jack Ma's instructor and find myself unfamiliar with this rewrite. The correct statement should be:
Public class coordinate {
Public int headx, heady;
Coordinate (INT headx, int heady ){
This. headx = headx;
This. Heady = heady;
}
Public int hashcode (){
Return 0;
}
Public Boolean equals (Object OBJ ){
If (OBJ = NULL ){
Return false;
} Else {
If (OBJ instanceof coordinate ){
Coordinate coo = (coordinate) OBJ;
If (this. headx = COO. headx & this. Heady = COO. Heady ){
Return true;
} Else {
Return false;
}
}
}
Return false;
}
}
Because the object type is passed, it is necessary to judge whether it is null and then determine whether it is the desired type:
OBJ instanceof coordinate;
If yes, you must first forcibly convert it to the type you want.
Coordinate coo = (coordinate) OBJ;
Then proceed to the judgment, so that the program is correct.
......
The rest is the rewriting of the hashcode method. It is also a line to rewrite the hashcode () method, so it is not clear when to rewrite the method. Yet to be studied ......
Below are the materials found by a blog on the Internet:
The equals method is an important object method. Rewrite follows the following principles:
1. self-inverse. For any reference, as long as the value is not null, The equals method is used for itself to always return true; 2. symmetry, for any reference A, B. if none of them are NUL, then. equals (B) returns B. equals (a); 3. passed, for any non-null reference A, B, C, if. equals (B) = true, B. equals (c) = true, then. equals (c) = true; 4. consistency. For any non-null reference of A and B, if the members involved in the equals comparison are not modified, perform multiple equals Comparison on A and B, and the results should always be the same; 5. Any non-null references a,. equals (null) defaults false; according to the above several rules, we can conclude that there are several steps to rewrite the equals method: 1. First, we should test whether two references point to the same object. if yes, true is returned. This step improves efficiency. 2. test whether the passed object is null. If yes, false is returned. 3. test whether the passed object Passes its instanc. EOF. If it does not pass, false is returned. 4. The passed object is forcibly converted to its own type and compared with related members. note: 1. the method modifier must be public because it is the method of the rewritten object. 2. the parameter type must be object.3. if equals is rewritten, The hashcode method must be rewritten. otherwise, two equivalent objects may obtain different hashcodes, which may have serious consequences when used in the collection framework. override hashcode method: 1. when the hashcode method is called multiple times on the same object during Java program running, if the members involved in the equals comparison on the object are not modified, the constant integer must be returned. 2. from execution of a program to another execution of the same program, this integer does not need to be consistent. 3. if the two objects call the hashcode method to return different values, the two objects must return false.4if The equals method returns true, the hashcode method must return the same value. 5. if equals returns false, then ha Different values are returned for the shcode ratio. * hashcode is mainly used to improve the query efficiency of the hash system. a. equals (B) = true must be. hashcode () = B. hashcode ();. hashcode! = B. hashcode, A. Equals (B) = false;