Table of Contents1 series Article address 2 a perfect equals method should contain content 3 modify part of the code in the Exchange Rate Converter 1 Series Article address java exchange rate calculator implementation (1) java exchange rate calculator Implementation (2) java exchange rate calculator implementation-Episode 1-regular expression (1) java exchange rate calculator implementation (3) java jsoup introduction-Implementation of the exchange rate calculator-Episode 2 java notes and javadoc introduction-Implementation of the exchange rate calculator-episode 32 the perfect equals method should contain the content first to judge whether the two objects are the same an object if (this = otherObject) return true; if the other object is null, return falseif (otherObject = null) return false; the types of the two classes to be compared should be the same if (getClass ()! = OtherObject. getClass () return false; however, some people may use the following method: if (! (OtherObject instanceof ThisClass) return false; this will cause a problem, such as: copying code // parent class People {public People (String name) {this. name = name;} public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; // if (getClass ()! = Obj. getClass () return false; if (! (Obj instanceof People) return false; People other = (People) obj; return name. equals (other. name);} private String name;} // subclass class Student extends People {public Student (String name, String studentID) {super (name); this. studentID = studentID;} public boolean equals (Object obj) {if (! Super. equals (obj) return false; Student s = (Student) obj; return studentID = s. studentID;} private String studentID;} copy the code. When you run the following example in the main function, the exception of ClassCastException will be thrown. Copy the code public class Demo {public static void main (String [] args) {People p1 = new People ("zhang "); student s1 = new Student ("zhang", "ID1"); System. out. println (s1.equals (p1) ;}} copy the code. Therefore, we recommend that you use: if (getClass ()! = OtherObject. getClass () return false; finally, convert the type to compare the size of each field: People other = (People) obj; return name. equals (other. name); 3. modify part of the code in the Exchange Rate Converter and modify the equals method in the Money class: copy the code/** when the currency unit and amount are equal, the two currency objects are equal: @ param obj currency @ return equals to <code> true </code>, not equal to <code> false </code> */public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Money other = (Money) obj; return amount = other. amount & unit. equals (other. unit );}