2016-04-30 11 views
-1

私の委員会のクラスでは、私はこれを行うには、どのように働く時間の支払いを計算するために、親クラス(時間単位)から支払方法を上書きしようとしていますか?親であるCommissionとHourlyクラスのコードは以下の通りですが、上書きはどこに置かれますか?スーパーメソッドを上書きすると

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

おそらく、doubleの代わりにBigDecimalを使うことも考えてください。 – Fildor

+0

あなたの質問には、「支払方法を無効にする」と書かれていますが、あなたのコードではそのようなことはしません - なぜですか?代わりに、新しいメソッド 'addSales(...)'を作成しています。これは、あなたが私たちのために全く定義していないメソッドです。 –

答えて

0

あなたのクラスに、あなたは、このようにpayのオーバーライドを追加しCommission

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

それは働いている時間の賃金を計算してから、その賃金に売上(総売上*手数料)の手数料を加えたものです。 –

関連する問題