2016-04-30 88 views
-1

在我的委員會類中,我試圖覆蓋從父類(小時)的薪酬方法來計算工作小時的薪酬,我將如何做到這一點?我的代碼爲委員會和小時班,這是父母,在下面,覆蓋發生在我把那個????覆蓋方法和使用超

public class Commission extends Hourly 
{ 
    double total_sales; 
    double commission_rate; 

    public Commission(String name, String address, String phone, 
        String soc_sec_number, double rate,double commission_rate) 
    { 
    super( name, address, phone, 
         soc_sec_number, rate); 
    // set commission rate 
    commission_rate = 0.02; 
    } 

    public void addSales (double sales) 
    { 
    sales += total_sales; 

    /*???? */super.pay 
    } 

} 

和每小時類

// ******************************************************************** 
// Hourly.java  Java Foundations 
// 
// Represents an employee that gets paid by the hour 
// ******************************************************************** 

public class Hourly extends Employee 
{ 
    private int hours_worked; 

    // ------------------------------------------------------------------------- 
    // Constructor: Sets up this hourly employee using the specified information 
    // ------------------------------------------------------------------------- 
    public Hourly(String name, String address, String phone, 
       String soc_sec_number, double rate) 
    { 
    super(name, address, phone, soc_sec_number, rate); 
    hours_worked = 0; 
    } 

    // ----------------------------------------------------- 
    // Adds the specified number of hours to this employee's 
    // accumulated hours 
    // ----------------------------------------------------- 
    public void addHours(int more_hours) 
    { 
    hours_worked += more_hours; 
    } 

    // ----------------------------------------------------- 
    // Computes and returns the pay for this hourly employee 
    // ----------------------------------------------------- 
    public double pay() 
    { 
    double payment = pay_rate * hours_worked; 

    hours_worked = 0; 
    return payment; 
    } 

    // ---------------------------------------------------------- 
    // Returns information about this hourly employee as a string 
    // ---------------------------------------------------------- 
    public String toString() 
    { 
    return super.toString() + "\nCurrent hours: " + hours_worked; 
    } 
} 
+0

請定義所需的行爲。 – MikeCAT

+0

也許你應該考慮使用BigDecimal而不是double。 – Fildor

+0

你的問題指出你「應該重寫付費方法」,但在你的代碼中你沒有這樣的事情 - 爲什麼?相反,你正在創建一個新的方法,'addSales(...)',你根本沒有爲我們定義 - 再次,爲什麼? –

回答

0

到類Commission您添加的pay倍率是這樣的:

@Override 
public double pay() { 
    double payment = super.pay(); // call the method of the parent 
    ....       // add other code 
} 
+0

它應該計算工作時間的工資,然後再加上銷售佣金(總銷售額*佣金率)的工資,我該怎麼做? –