Skip to content

Commit f94de1d

Browse files
committed
完成Object超类
1 parent 39eddc2 commit f94de1d

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

Part5/equals/Employee.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

Part5/equals/EqualsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package equals;
2+
3+
public class EqualsTest {
4+
public static void main(String[] args) {
5+
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
6+
Employee alice2 = alice1;
7+
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
8+
Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
9+
10+
System.out.println("alice1 == alice2: " + (alice1 == alice2));
11+
12+
System.out.println("alice1 == alice3: " + (alice1 == alice3));
13+
14+
System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
15+
16+
System.out.println("alice1.equals(bob): " + alice1.equals(bob));
17+
18+
System.out.println("bob.toString(): " + bob);
19+
20+
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
21+
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
22+
boss.setBonus(5000);
23+
System.out.println("boss.toString(): " + boss);
24+
System.out.println("carl.equals(boss): " + carl.equals(boss));
25+
System.out.println("alice1.hashCode(): " + alice1.hashCode());
26+
System.out.println("alice3.hashCode(): " + alice3.hashCode());
27+
System.out.println("bob.hashCode(): " + bob.hashCode());
28+
System.out.println("carl.hashCode(): " + carl.hashCode());
29+
}
30+
}

Part5/equals/Manager.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package equals;
2+
3+
public class Manager extends Employee {
4+
private double bonus;
5+
6+
public Manager(String n, double s, int year, int month, int day) {
7+
super(n, s, year, month, day);
8+
bonus = 0;
9+
}
10+
11+
public double getSalary() {
12+
double baseSalary = super.getSalary();
13+
return baseSalary + bonus;
14+
}
15+
16+
public void setBonus(double b) {
17+
bonus = b;
18+
}
19+
20+
public boolean equals(Object otherObject) {
21+
if (!super.equals(otherObject)) return false;
22+
Manager other = (Manager) otherObject;
23+
return bonus == other.bonus;
24+
}
25+
26+
public int hashCode() {
27+
return super.hashCode() + 17 * new Double(bonus).hashCode();
28+
}
29+
30+
public String toString() {
31+
return super.toString() + "[bonus=" + bonus + "]";
32+
}
33+
}

Part5/inheritance/Manager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
* 需要注意的是:
66
* 在Java中,所有的继承都是公用继承,
77
* 而没有C++中的私有继承和保护继承
8+
*
9+
* 在这里顺路总结一下Java中用于控制可见性的4个访问修饰符:
10+
* 1. private -- 仅对本类可见
11+
* 2. public -- 对所有类可见
12+
* 3. protected -- 对本包和所有子类可见
13+
* 4. 没有修饰符的情况下。默认对本包可见
814
*/
915
public class Manager extends Employee {
1016
private double bonus;

README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@
5858
|
5959
| ----- abstractClasses(抽象类)
6060
|
61+
| ----- equals(Object超类)
62+
|
6163
| ----- ...

0 commit comments

Comments
 (0)