|
| 1 | +package equals; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.util.GregorianCalendar; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +public class Employee |
| 8 | +{ |
| 9 | + private String name; |
| 10 | + private double salary; |
| 11 | + private Date hireDay; |
| 12 | + |
| 13 | + /* 构造方法 */ |
| 14 | + public Employee(String n, double s, int year, int month, int day) { |
| 15 | + name = n; |
| 16 | + salary = s; |
| 17 | + GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); |
| 18 | + hireDay = calendar.getTime(); |
| 19 | + } |
| 20 | + |
| 21 | + public String getName() { |
| 22 | + return name; |
| 23 | + } |
| 24 | + |
| 25 | + public double getSalary() { |
| 26 | + return salary; |
| 27 | + } |
| 28 | + |
| 29 | + public Date getHireDay() { |
| 30 | + return hireDay; |
| 31 | + } |
| 32 | + |
| 33 | + public void raiseSalary(double byPercent) { |
| 34 | + double raise = salary * byPercent / 100; |
| 35 | + salary += raise; |
| 36 | + } |
| 37 | + |
| 38 | + /* |
| 39 | + * equals方法的实现实例; |
| 40 | + * |
| 41 | + * 在这里记下《Core Java Volumn I》中编写一个完美的equals方法哦的建议: |
| 42 | + * 1. 显式参数命名为otherObject,稍后需要将她转换成另一个叫做other的变量 |
| 43 | + * 2. 检测this与otherObject是否引用同一个对象: |
| 44 | + * if (this == otherObject) return true; |
| 45 | + * 3. 检测otherObject是否为null,如果为null,返回false。这项检测是很有必要的 |
| 46 | + * 4. 比较this与otherObject是否属于同一个类。如果equals的语义在每个子类中有所改变,就使用getClass检测: |
| 47 | + * if (getClass() != otherObject.getClass()) return false; |
| 48 | + * 如果所有的子类都拥有统一的语义,就使用instanceof检测: |
| 49 | + * if(!(otherObject instanceof ClassName)) return false; |
| 50 | + * 5. 将otherObject转换为相应的类类型变量: |
| 51 | + * ClassName other = (ClassName) otherObject |
| 52 | + * 6. 现在开始对所有需要比较的域进行比较了。使用 == 比较基本类型域,使用equals比较对象域。 |
| 53 | + * 如果所有的域都匹配,就返回true;否则返回false。 |
| 54 | + * return field1 == other.field1 |
| 55 | + * && Object.equals(field2, other.field2) |
| 56 | + * && ... |
| 57 | + * 如果在子类中重新定义了equals,就要在其中包含调用super.equals(other) |
| 58 | + */ |
| 59 | + public boolean equals(Object otherObject) { |
| 60 | + if (this == otherObject) return true; |
| 61 | + |
| 62 | + if (otherObject == null) return false; |
| 63 | + |
| 64 | + if (getClass() != otherObject.getClass()) return false; |
| 65 | + |
| 66 | + Employee other = (Employee) otherObject; |
| 67 | + |
| 68 | + return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + * hashCode方法; |
| 73 | + * 散列码(hash code)是由对象导出的一个整型值, |
| 74 | + * 散列码是没有规律的; |
| 75 | + * 如果x和y是两个不同的对象,x.hashCode()和y.hashCode()基本上不会相同; |
| 76 | + * 注意,equals方法和hashCode方法的定义必须一致 |
| 77 | + */ |
| 78 | + public int hashCode() { |
| 79 | + return Objects.hash(name, salary, hireDay); |
| 80 | + } |
| 81 | + |
| 82 | + /* 强烈建议为每一个自定义的类增加一个toString方法 */ |
| 83 | + public String toString() { |
| 84 | + return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay |
| 85 | + + "]"; |
| 86 | + } |
| 87 | +} |
0 commit comments