Write the execution results of polymorphism code
寫出多型程式碼的執行結果
、根據下列程式回答問題:
Employee, HourlyEmployee, SalariedEmployee 三個類別的關係為何?(5 分)
試指出一個建構子,並說明它的作用。(5 分)
試說明 SalariedEmployee 中 super(n)的作用。(5 分)
試指出多型(polymorphism)出現的地方。(5 分)
執行 PayrollSystemTest 後印出的結果為何?(10 分)
public abstract class Employee
{ private String name;
public Employee( String n )
{ name = n;
}
public void setName( String str )
{ name = str;
}
public String getName()
{ return name;
}
@Override
public String toString()
{ return String. format( "%s", getName() );
}
public abstract double earnings();
}
public class HourlyEmployee extends Employee
{ private double wage; // wage per hour
private double hours; // hours worked for week
public HourlyEmployee( String n, double hourlyWage, double hoursWorked )
{ super( n );
setWage( hourlyWage );
setHours( hoursWorked );
}
public void setWage( double hourlyWage )
{ if ( hourlyWage >= 0.0 )
wage = hourlyWage;
else
throw new IllegalArgumentException("Hourly wage must be >= 0.0" );
}
public double getWage()
{ return wage;
}
public void setHours( double hoursWorked )
{ if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) )
hours = hoursWorked;
else
throw new IllegalArgumentException("Hours worked must be >= 0.0 and <= 168.0" );
}
public double getHours()
{ return hours;
}
@Override
public double earnings()
{ if ( getHours() <= 40 ) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
}
@Override
public String toString()
{ return String. format( "hourly employee: %s\n%s: $%,.2f; %s: %, .2f",
super.toString(), "hourly wage", getWage(),"hours worked", getHours() );
}
}
public class SalariedEmployee extends Employee
{ p rivate double weeklySalary;
public SalariedEmployee( String n, double salary )
{ super( n );
setWeeklySalary( salary );
}
public void setWeeklySalary( double salary )
{ if ( salary >= 0.0 )
weeklySalary = salary;
else
throw new IllegalArgumentException( "Weekly salary must be >= 0.0" );
}
public double getWeeklySalary()
{ return weeklySalary;
}
@Override
public double earnings()
{ return getWeeklySalary();
}// end method earnings
@Override
public String toString()
{ return String. format( "salaried employee: %s\n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee
public class PayrollSystemTest
{ public static void main( String[] args )
{ // create subclass objects
SalariedEmployee salariedEmployee = new SalariedEmployee( "Smith", 800.00 );
HourlyEmployee hourlyEmployee = new HourlyEmployee( "Price", 16.75, 40 );
System.out.println( "Employees processed individually:\n" );
System.out.printf( "%s\n%s: $%,.2f\n\n",
salariedEmployee, "earned", salariedEmployee.earnings() );
System.out.printf( "%s\n%s: $%,.2f\n\n",hourlyEmployee, "earned", hourlyEmployee.earnings() );
Employee[] employees = new Employee[ 2 ];
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
for ( Employee currentEmployee : employees )
{ System.out.println( currentEmployee );
System.out.printf( "earned $%, .2f\n\n", currentEmployee.earnings() );
} // end for
} // end main
} // end class PayrollSystemTest
1. **Employee, HourlyEmployee, SalariedEmployee 三個類別的關係**:
- `Employee` 是一個抽象基類,代表所有員工的共同特性,但不能被實例化。
- `HourlyEmployee` 和 `SalariedEmployee` 是 `Employee` 的派生類別,它們繼承了 `Employee` 的屬性和方法,並提供了自己的實作以表示不同類型的員工。這是一種繼承(inheritance)關係。
2. **一個建構子及其作用**:
- `Employee` 類別有一個建構子 `public Employee(String n)`,它接受一個字串參數 `n`,用來設定員工的名字 (`name`)。這個建構子在物件被創建時設定員工的名字。
3. **SalariedEmployee 中 super(n) 的作用**:
- `super(n)` 用於調用父類別 `Employee` 的建構子,並將名字 `n` 傳遞給父類別的建構子,以設定員工的名字。這確保了所有類別都可以設定員工的名字。
4. **多型(polymorphism)出現的地方**:
- 多型出現在 `PayrollSystemTest` 的 `for` 迴圈中,當我們迭代 `Employee` 陣列時,實際上在運行時會根據物件的實際型別(`SalariedEmployee` 或 `HourlyEmployee`)調用正確的 `earnings` 方法。這是由於 `Employee` 類別中聲明的 `earnings` 方法是抽象的,並在子類別中實現。這種能力允許我們以統一的方式處理不同型別的員工。
5. **執行 PayrollSystemTest 後印出的結果**:
- 程式首先創建一個 `SalariedEmployee` 物件 `salariedEmployee` 和一個 `HourlyEmployee` 物件 `hourlyEmployee`。
- 然後,分別計算和印出每個員工的收入。
- 最後,使用 `for` 迴圈遍歷一個 `Employee` 陣列,並印出每個員工的名字和收入。
- 結果是:
```
Employees processed individually:
salaried employee: Smith
weekly salary: $800.00
earned $800.00
hourly employee: Price
hourly wage: $16.75; hours worked: 40.00
earned $670.00
salaried employee: Smith
earned $800.00
hourly employee: Price
earned $670.00
```
Comments
Post a Comment