Java 覆盖方法和使用超级
在我的佣金课程中,我试图覆盖父类(小时)的pay方法来计算工时工资,我将如何做到这一点?下面是我的佣金和小时班代码,它是家长,覆盖发生在我放置???的地方Java 覆盖方法和使用超级,java,methods,super,overwrite,Java,Methods,Super,Overwrite,在我的佣金课程中,我试图覆盖父类(小时)的pay方法来计算工时工资,我将如何做到这一点?下面是我的佣金和小时班代码,它是家长,覆盖发生在我放置???的地方 public class Commission extends Hourly { double total_sales; double commission_rate; public Commission(String name, String address, String phone,
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;
}
}
在您的班级
佣金
中添加支付
的覆盖,如下所示:
@Override
public double pay() {
double payment = super.pay(); // call the method of the parent
.... // add other code
}
请定义所需的行为。也许你也应该考虑使用BigDigPiple而不是双。你的问题表明你应该“重写支付方法”,但是在你的代码中你不做这样的事情——为什么?相反,您正在创建一个新方法,
addSales(…)
,您根本没有为我们定义它——再说一遍,为什么?@MikeCAt您想要的行为是什么意思???被重写的pay方法必须调用父类中的pay方法来计算工时工资,然后将来自销售佣金的工资(totals sales乘以佣金率)添加到该工资中计算付款后,应将总销售额重置为0。@HoverCraftFullOfels addSales方法将参数添加到表示总销售额的实例变量中。你如何从每小时计算佣金?它应该计算工作小时的工资,然后再加上销售佣金(总销售额*佣金率)的工资。我该怎么做?