[Java] Better equals Writing Method-Implementation of the exchange rate calculator (4)

Source: Internet
Author: User

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 );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.