Skip to content

Commit e2202b5

Browse files
committed
完成Part5/inheritance
1 parent 9220d32 commit e2202b5

4 files changed

Lines changed: 27 additions & 43 deletions

File tree

Part5/inheritance/Employee.java

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

6-
public class Employee
7-
{
6+
public class Employee {
87
private String name;
98
private double salary;
109
private Date hireDay;
1110

12-
public Employee(String n, double s, int year, int month, int day)
13-
{
11+
public Employee(String n, double s, int year, int month, int day) {
1412
name = n;
1513
salary = s;
1614
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
1715
hireDay = calendar.getTime();
1816
}
1917

20-
public String getName()
21-
{
18+
public String getName() {
2219
return name;
2320
}
2421

25-
public double getSalary()
26-
{
22+
public double getSalary() {
2723
return salary;
2824
}
2925

30-
public Date getHireDay()
31-
{
26+
public Date getHireDay() {
3227
return hireDay;
3328
}
3429

35-
public void raiseSalary(double byPercent)
36-
{
30+
public void raiseSalary(double byPercent) {
3731
double raise = salary * byPercent / 100;
3832
salary += raise;
3933
}

Part5/inheritance/Manager.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
package inheritance;
22

3-
public class Manager extends Employee
4-
{
3+
/* Java中使用extends关键字来表示“继承”的概念,
4+
* extends关键字表明正在构造的新类派生于一个已经存在的类;
5+
* 需要注意的是:
6+
* 在Java中,所有的继承都是公用继承,
7+
* 而没有C++中的私有继承和保护继承
8+
*/
9+
public class Manager extends Employee {
510
private double bonus;
611

7-
/**
8-
* @param n the employee's name
9-
* @param s the salary
10-
* @param year the hire year
11-
* @param month the hire month
12-
* @param day the hire day
12+
/* 下面这段代码块使用了super来调用其父类(超类)的构造器,
13+
* super是一个指示编译器调用超类方法的特殊关键字;
14+
* 注意super并不是一个对象的引用,不能将super赋给另一个变量对象
1315
*/
14-
public Manager(String n, double s, int year, int month, int day)
15-
{
16+
public Manager(String n, double s, int year, int month, int day) {
1617
super(n, s, year, month, day);
1718
bonus = 0;
1819
}
1920

20-
public double getSalary()
21-
{
21+
/* 在这里对其超类(Employee)中的getSalary方法进行了重写(override) */
22+
public double getSalary() {
2223
double baseSalary = super.getSalary();
2324
return baseSalary + bonus;
2425
}
2526

26-
public void setBonus(double b)
27-
{
27+
public void setBonus(double b) {
2828
bonus = b;
2929
}
3030
}
31-

Part5/inheritance/ManagerTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
package inheritance;
22

3-
4-
/**
5-
* This program demonstrates inheritance.
6-
* @version 1.21 2004-02-21
7-
* @author Cay Horstmann
8-
*/
9-
public class ManagerTest
10-
{
11-
public static void main(String[] args)
12-
{
13-
// construct a Manager object
3+
public class ManagerTest {
4+
public static void main(String[] args) {
5+
/* 构造一个对象 */
146
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
157
boss.setBonus(5000);
168

179
Employee[] staff = new Employee[3];
1810

19-
// fill the staff array with Manager and Employee objects
20-
2111
staff[0] = boss;
2212
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
2313
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);
2414

25-
// print out information about all Employee objects
2615
for (Employee e : staff)
2716
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
2817
}
29-
}
18+
}

README

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@
5454

5555
Part5(继承,2014年04月23日 -- ?)
5656
|
57-
|----- ...
57+
| ----- inheritance(继承初探)
58+
|
59+
| ----- ...

0 commit comments

Comments
 (0)