Overriding the Equals method in the object class

Source: Internet
Author: User

Object is the father of all classes, this class has many methods, we can call directly, but some methods are not suitable, such as the following student class

1  Public classStudent {2     //name, school number, age3     PrivateString name;4     Private intSID;5     Private intAge ;6     7     //define the constructor method to initialize the object8      PublicStudent () {9         Ten     } One      PublicStudent (String name,intSidintAge ) { A          This. name=name; -          This. sid=SID; -          This. age=Age ; the     } -      -      PublicString GetName () { -         returnname; +     } -      Public voidsetName (String name) { +          This. Name =name; A     } at      Public intGetSID () { -         returnSID; -     } -      Public voidSetsid (intSID) { -          This. Sid =SID; -     } in      Public intGetage () { -         returnAge ; to     } +      Public voidSetage (intAge ) { -          This. Age =Age ; the     } *}

Test class:

1  PackageCom.cn.reequlas;2 //equals is to compare 2 objects for equality, because STU1 and STU2 are 2 pairs of images, within which 2 addresses are stored, so not the same, looking for memory addresses,3  Public classteststudent {4      Public Static voidMain (string[] args) {5Student stu1=NewStudent ("Zhang San",);6Student stu2=NewStudent ("Zhang San",);7                 Booleanflag=stu1.equals (STU2);8 System.out.println (flag);9     }Ten      One}

Because for the Equals method in the object class, it is used to compare the 2 pairs of images in the memory of the address is the same, it is clear that stu1 new, STU2 is also a new address, of course, different

In practical applications, 2 students are called Zhang San, the school number is the same, because the study number is unique, so they are the same person. Then we have to rewrite the Equals method, and when the name is the same as the school number, it's the same pair.

Here's a point of knowledge: instanceof

// instanceof refers to obj as belonging to student        if instanceof Student))

Add the overridden Equals method as follows:

1  PackageCom.cn.reequlas;2 3  Public classStudent {4     //name, school number, age5     PrivateString name;6     Private intSID;7     Private intAge ;8     9     //define the constructor method to initialize the objectTen      PublicStudent () { One          A     } -      PublicStudent (String name,intSidintAge ) { -          This. name=name; the          This. sid=SID; -          This. age=Age ; -     } -      +      PublicString GetName () { -         returnname; +     } A      Public voidsetName (String name) { at          This. Name =name; -     } -      Public intGetSID () { -         returnSID; -     } -      Public voidSetsid (intSID) { in          This. Sid =SID; -     } to      Public intGetage () { +         returnAge ; -     } the      Public voidSetage (intAge ) { *          This. Age =Age ; $     }Panax Notoginseng     //rewrite the Equlas method, judge the name, the school number is equal, is the same student -     //obj was given to one of our students on the like the     //This is our own student on like equals +      A      Public Booleanequals (Object obj) { the         Booleanflag=false; +         //It is impossible to judge whether the same is the same as the -         if(obj== This){ $flag=true; $         } -         // -         if(! (objinstanceofStudent)) { theflag=false; -}Else{WuyiStudent stu=(Student) obj; the             if(Stu.name.equals ( This. Name) && stu.sid== This. Sid) { -flag=true; Wu             } -         } About System.out.println (flag); $         returnFlag; -     } -}

The test class does not change, the output is true

 PackageCom.cn.reequlas;//equals is to compare 2 objects for equality, because STU1 and STU2 are 2 pairs of images, within which 2 addresses are stored, so not the same, looking for memory addresses, Public classteststudent { Public Static voidMain (string[] args) {Student stu1=NewStudent ("Zhang San",); Student STU2=NewStudent ("Zhang San",); /*Pet pet=new Pet ("Zhang San");*/        //using the rewritten Equlas method, the 1th if inside, to determine whether the same as, this (STU1) is obj,stu1 is this        /*boolean flag1=stu1.equals (STU1);        System.out.println (FLAG1); */        //Verify that the 2nd if of the overridden object method in the student class is not of the same type        /*Boolean flag2=stu1.equals (PET); System.out.println (FLAG2);*/        //Verify that the else in the 2nd if of overriding the object method in the student class        /*boolean flag4=stu1.getname (). Equals (Stu2.getname ());        System.out.println (FLAG4);        Boolean flag5=stu1.getsid () = = (Stu2.getsid ()); System.out.println (FLAG5);*/                        Booleanflag=stu1.equals (STU2);    SYSTEM.OUT.PRINTLN (flag); }    }

Overriding the Equals method in the object class

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.