Java: Verifying the use of the Equals (), Hashcode (), ToString () methods during class inheritance

Source: Internet
Author: User
Tags class manager create domain

The following is a practical example of the common Equals (), Hashcode (), toString () methods used in the class creation process sinks, and the creation process of the three methods is universal and can be rewritten directly in the project.


//through the superclass employee and its subclass manager, verify the use of the Equals (), Hashcode (), ToString () methods during class inheritance.
Package equals;
Public class equalstest{
Public static void Main (string[] args) {
Employee Alice1=new Employee ("Alice Adams", 75000,1987,12,15);
Employee alice2=alice1;//Object Replication
Employee Alice3=new Employee ("Alice Adams", 75000,1987,12,15);
Employee Bob=new Employee ("Bob Brandson", 50000,1989,10,1);
System.out.println ("Alice1==alice2:" + (ALICE1==ALICE2));
System.out.println ("Alice1==alice3:" + (Alice1==alice3));
System.out.println ("Alice1.equals (alice3):" +alice1.equals (Alice3));
System.out.println ("Alice1.equals (Bob):" +alice1.equals (Bob));
System.out.println ("Bob.tostirng ():" +bob);//Use the ToString () method and the string form of the output Bob object
        
//Create object variables for sub-class manager
Manager carl=new Manager ("Carl Cracker", 80000,1987,12,15);
Manager boss=new Manager ("Carl Cracker", 80000,1987,12,15);
Boss.setbluns (the);
        
System.out.println ("boss.tostring ():" +boss);
System.out.println ("Carl.equals (boss):" +carl.equals (Boss));
System.out.println ("Alice1.hashcode ():" +alice1.hashcode ());

       //call Hashcode () method to export the hash code of the object
        system.out.println (" Alice3.hashcode (): "+alice3.hashcode ()) ;
        system.out.println ("Boss.hashcode ():" + Boss.hashcode ());
        system.out.println ("Carl.hashcode ():" + Carl.hahscode ());
        
        
    }
}


Create Super Class Employee
Package equals;
Import Java.util.Date;
Import Java.util.GregorianCalendar;
Import java.util.Objects;

Public class employee{
    
private String name;
private double salary;
Private Date hireday;//Create class object variable
    
//Create super Class constructors
Public Employee (String n,double s,int year,int month,int day) {
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar (year,month-1,day);//Create an object variable with a constructor with parameters
hireday=calendar.gettime ();
    }
    
//create domain accessor and change method
Public String GetName () {
return name;
    }
    
Public Double Getsalary () {
return salary;
    }
    
Public Date Gethireday () {
return hireday;
    }
    
//Create Method Raisesalary ()
Public void Raisesalary (double bypercent) {
double raise=salary*bypercent/100;
salary+=raise;
        
    }
    
//Create Method equals ()
Public Boolean equals (Object otherobject) {
if (this==otherobject) {//detects if this and otherobject refer to the same object
return true;
        }
        
if (otherobject==null) {//detects if an explicit parameter is NULL, then returns false
return false;
        }
        
if (GetClass ()!=otherobject.getclass ()) {
//Compare if this and otherobject belong to the same class, and if they do not belong to the same class, then the Equals () operation cannot be performed

return false;
        }
        
//Convert Otherobject to corresponding class-type variable
Employee other= (employee) Otherobject;
        
//Start comparing the fields you need to compare now
return Objects.equals (name,other.name)
&&objects.equals (salary,other.salary)//&&salary==other.salary is this possible?
&&objects.equals (hireday,other.hireday);
        
                
    }
    
public int Hashcode () {
the//objects.hash () method provides multiple parameters,
//Call the Objects.hashcode () method on each parameter to get the respective hash value and combine the hash values.
return Objects.hash (name,salary,hireday);
        
    }
    
Public String toString () {
return GetClass (). GetName () + "[name" +name+ "salary=" +salary+ "hireday=" +hireday+ "]";
        
    }
    
}


Create employee sub-class manager
Package equals;

Public class Manager extends employee{
Private Double bouns;//creates a new instance domain of the subclass itself
Public Manager (String n,double s,int year.int month,int day) {
super (N,s,year,month,day);//Call the constructor of a super class
bouns=0;
    }
    
Public Double Getsalary () {//overwrite Getsalary () method for Super class employee
double basesalary=super.getsalary ();//Call the Superclass getsalary ()
return basesalary+bouns;
    }
    
Public void Setbouns (double a) {
Bouns=a;
    }
    
Public Boolean equals (Object otherobject) {//overwrite the Equals () method of the superclass employee
if (!super.equals (Otherobject)) {
return false;
            
        }
        
Manager other= (manager) Otherobject;
return bouns==other.bouns;
        
    }
    
public int Hashcode () {
return Super.hashcode () +17*new Double (bouns). Hashcode ();
        
    }
    
Public String toString () {
return super.tostring () + "[bouns=" +bouns+ "]";
        
    }
}

Java: Validating the use of the Equals (), Hashcode (), ToString () methods during class inheritance

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.