【问题标题】:overwriting methods and using super [duplicate]覆盖方法并使用超级[重复]
【发布时间】:2023-03-20 23:30:01
【问题描述】:

在我的 Commission 类中,我试图覆盖父类(Hourly)中的 pay 方法来计算工作小时数的工资,我该怎么做?我的 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;
  }
}

【问题讨论】:

  • 请定义所需的行为。
  • 也许您还应该考虑使用 BigDecimal 而不是 double。
  • 您的问题表明您“应该覆盖付款方式”但在您的代码中您没有这样做 - 为什么?相反,您正在创建一个新方法,addSales(...),您根本没有为我们定义 - 再说一遍,为什么?
  • @MikeCAt 你想要的行为是什么意思???覆盖的 pay 方法必须从父类调用 pay 方法来计算工作时间的工资,然后加上销售佣金的工资(总销售额乘以佣金率) 付款后总销售额应重置为 0计算出来的。
  • @HovercraftFullOfEels addSales 方法将参数添加到代表总销售额的实例变量中。您将如何覆盖每小时佣金中的支付方式?

标签: java methods super overwrite


【解决方案1】:

在您的班级Commission 中添加pay 的覆盖,如下所示:

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

【讨论】:

  • 它应该计算工作时间的工资,然后加上销售佣金的工资(总销售额*佣金率)我该怎么做?
猜你喜欢
  • 1970-01-01
  • 2016-10-23
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多