java.lang.Object
Class getClass()
returns a class object that contains information about the object. As you will see later in this chapter, Java has a runtime representation for classes that is encapsulated in the Class class.
boolean equals(Object otherObject)
compares two objects for equality: returns true if the objects point to the same area of memory, and false otherwise. You should override this method in your own classes.
String toString()
returns a string that represents the value of this object. You should override this method in your own classes.
java.lang.Class
String getName()
return the name of this class.
Class getSuperClass of this class as a Class Object.
/**
* Created by wang on 17-7-10.
*/
public class Manager extends Employee {
private double bonus;
public Manager(String name, double salary, int year, int month, int day) {
super(name, salary, year, month, day);
bonus = 0;
}
@Override
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) return false;
Manager other = (Manager) obj;
return bonus == other.bonus;
}
@Override
public int hashCode() {
return super.hashCode() + 17 * new Double(bonus).hashCode();
}
@Override
public String toString() {
return super.toString() + "[bonus=" + bonus + "]";
}
}
import java.time.LocalDate;
import java.util.Objects;
/**
* Created by wang on 17-7-10.
*/
public class Employee {
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary, int year, int month, int day) {
this.name = name;
this.salary = salary;
this.hireDay = LocalDate.of(year, month, day);
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public LocalDate getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
@Override
public boolean equals(Object obj) {
// a quick test to see if the objects are identical
if (this == obj) return true;
// must return false if the explicit parameter is null
if (obj == null) return false;
// if the classes don't match, they can't be equal
if (getClass() != obj.getClass()) return false;
// now we know obj is a non-null Employee
Employee other = (Employee) obj;
// test wheter the fields have identical values
return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, ((Employee) obj).hireDay);
}
@Override
public int hashCode() {
return Objects.hash(name, salary, hireDay);
}
@Override
public String toString() {
return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
/**
* Created by wang on 17-7-10.
*/
public class EqualsTest {
public static void main(String[] args) {
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
Employee alice2 = alice1;
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.toString():" + bob);
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);
System.out.println("boss.toString():" + boss);
System.out.println("carl.equals(boss):"+ carl.equals(boss));
System.out.println("alice1.hashCode():" + alice1.hashCode());
System.out.println("alice3.hashCode():" + alice3.hashCode());
System.out.println("bob.hashCode():" + bob.hashCode());
System.out.println("carl.hashCode():" + carl.hashCode());
}
}
alice1 == alice2:true
alice1 == alice3:false
alice1.equals(alice3):true
alice1.equals(bob):false
bob.toString():Employee[name=Bob Brandson,salary=50000.0,hireDay=1989-10-01]
boss.toString():Manager[name=Carl Cracker,salary=80000.0,hireDay=1987-12-15][bonus=5000.0]
carl.equals(boss):false
alice1.hashCode():-808853550
alice3.hashCode():-808853550
bob.hashCode():-624019882
carl.hashCode():-341762419