*overriding the ToString method*when we need to use the ToString method of the current class, we usually*you will need to override this method. The format of the specific return string is not strictly*requirements, depending on the needs of the actual development in the future. But usually anti-*The property value of the current class should be included in the returned string to understand the current*the information for the object. *format: (x, y) PublicString toString () {return"(" + x + "," + y + ")"; } *usually we rewrite the Equals method of a class. *The purpose of the Equals method is to compare the current object to the given object's*whether the content is the same (property value)* If you do not rewrite it is provided using object, and its internal is "= =" *realized, so it makes no sense. *the Equals method does not necessarily require that the content of two objects be more consistent*all property values are the same, depending on the actual demand. Public Booleanequals (Object obj) {if(obj = =NULL){ return false; } if(obj = = This){ return true; } if(objinstanceofPoint ) {Point P=(point) obj; return This. x==p.x && This. y==p.y; } return false; } * "=="when comparing two reference types, the comparison is the same*object System.out.println (P= = p1);//false* "Equals"when comparing two reference types, the content is compared*is the same. (Not like) System.out.println (P.equals (p1));//true
Java class notes------tostring Method and Equals method