Skip to content

Commit df741cd

Browse files
committed
更新
1 parent ea45463 commit df741cd

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

Part5/abstractClasses/Employee.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.Date;
44
import java.util.GregorianCalendar;
55

6+
/* 因为Employee类中实现了Person抽象类中的所有抽象方法,
7+
* 所以不需要使用abstract关键字进行修饰
8+
*/
69
public class Employee extends Person {
710
private double salary;
811
private Date hireDay;
@@ -22,6 +25,7 @@ public Date getHireDay() {
2225
return hireDay;
2326
}
2427

28+
/* 这里对Person抽象类中的getDescription抽象方法进行了重写 */
2529
public String getDescription() {
2630
return String.format("an employee with a salary of $%.2f", salary);
2731
}

Part5/abstractClasses/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434

3535
public abstract class Person {
36-
36+
/* 这里就定义了一个抽象方法 */
3737
public abstract String getDescription();
3838
private String name;
3939

Part5/abstractClasses/Student.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package abstractClasses;
22

3+
/* 因为Employee类中实现了Person抽象类中的所有抽象方法,
4+
* 所以不需要使用abstract关键字进行修饰
5+
*/
36
public class Student extends Person {
47
private String major;
58

69
public Student(String n, String m) {
7-
// pass n to superclass constructor
810
super(n);
911
major = m;
1012
}
1113

14+
/* 这里对Person抽象类中的getDescription抽象方法进行了重写 */
1215
public String getDescription() {
1316
return "a student majoring in " + major;
1417
}

Part5/inheritance/Manager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public Manager(String n, double s, int year, int month, int day) {
1818
bonus = 0;
1919
}
2020

21-
/* 在这里对其超类(Employee)中的getSalary方法进行了重写(override) */
21+
/* 在这里对其超类(Employee)中的getSalary方法进行了重写(override);
22+
* 由于在Manager类中也提供了getSalary方法,
23+
* 相当于将Employee类中的getSalary方法进行了覆盖,
24+
* 当直接调用getSalary类的时候,实际上是调用Manager类中的getSalary,
25+
* 而不是Employee类中的getSalary;
26+
* 所以如果要使用Employee类中提供的getSalary方法,需要使用super关键字
27+
*/
2228
public double getSalary() {
2329
double baseSalary = super.getSalary();
2430
return baseSalary + bonus;

0 commit comments

Comments
 (0)