I have a simple custom square that holds only two coordinates.
Public stable class CoordPoint {Public Int X; Public spacing; Public CoordPoint () {} Public CoordPoint (int X, int Y) {this.X = X; this. Y = Y; }}
When two objects of these objects are equal to me, it does not pass the following statement.
CoordPoint curposition = new CoordPoint (5,5); CoordPoint Destination = New CoordPoint (5,5);
How can I evaluate my custom object?
You need to override the equals ()
method in your class so that Make proper comparisons. At the moment, if you do not override it, then behaves in the same way as
@ Override public boolean equivalent (object object) {return this == Other; }
In your case, it is not good if you are comparing it to context , then this is what is true
Returns I, your code:
CoordPoint curposition = new CoordPoint (5, 5); CoordPoint Destination = New CoordPoint (5, 5); If (curposition.equals (destination)) {/ * it will not be executed, since there are members * values of objects, two references are not pointing to the same object CoordPoint anotherOne = curposition; If (curposition.equals (anotherOne)) {// it will be executed, because the two references point to the same object. }
The purpose for which you intend, it will not work. One way to properly implement this method would be:
@ Override public boolean equivalent (object object) {if (other == blank) returned false; If ((other examples of CoordPoint)) returned incorrectly; CoordPoint Other Point = (CoordPoint) Other; Return (this.X == Other Point.X) & amp; Amp; (This is Y == other point. Y); }
The first line check is important; Otherwise, every time other
was zero you will get a NullPointerException in line, where you return the result when you tried to use a member of other
.
Comments
Post a Comment