Java中HourlyEmployee类super()调用报错:实参与形参长度不符求助
Hey there, let's break down why that super(hourly) call is throwing an error and how to fix it. That error message essentially means: the parent class doesn't have a constructor that accepts exactly the number and type of arguments you're passing to super().
What's going on here?
When you call super(hourly) in your HourlyEmployee copy constructor, you're trying to invoke a constructor in the parent class (let's assume it's something like Employee) that takes a single HourlyEmployee (or its parent type) object as input. But your parent class probably doesn't have that constructor defined—instead, it might only have a constructor that requires multiple individual parameters (like name, employee ID, base pay, etc.), or no copy constructor at all.
Two ways to fix this:
Option 1: If your parent class has a copy constructor
First, check if your parent class (e.g., Employee) has a copy constructor that accepts an instance of itself, like this:
public class Employee { private String name; private int employeeId; private double basePay; // Copy constructor for Employee public Employee(Employee other) { this.name = other.name; this.employeeId = other.employeeId; this.basePay = other.basePay; } }
If this exists, your super(hourly) call should work! Because HourlyEmployee is a subclass of Employee, it can be upcast to Employee and passed to this constructor. Just don't forget to copy the subclass-specific fields too:
public HourlyEmployee(HourlyEmployee hourly) { super(hourly); // This works if parent has a copy constructor // Copy HourlyEmployee-specific fields this.hourlyRate = hourly.hourlyRate; this.hoursWorked = hourly.hoursWorked; }
Option 2: If your parent class doesn't have a copy constructor
If your parent class only has a standard multi-parameter constructor, you need to pass those specific values to super() instead of the entire hourly object. For example:
public class Employee { private String name; private int employeeId; private double basePay; // Standard multi-parameter constructor public Employee(String name, int employeeId, double basePay) { this.name = name; this.employeeId = employeeId; this.basePay = basePay; } }
Then your HourlyEmployee copy constructor would look like this:
public HourlyEmployee(HourlyEmployee hourly) { // Call the parent's constructor with values extracted from the hourly object super(hourly.getName(), hourly.getEmployeeId(), hourly.getBasePay()); // Copy subclass-specific fields this.hourlyRate = hourly.hourlyRate; this.hoursWorked = hourly.hoursWorked; }
This matches the exact parameter count and types that the parent class's constructor expects, which will resolve the "arguments differ in length" error.
Quick sanity check:
Double-check all constructors in your parent class—none of them will have a single parameter that can accept a HourlyEmployee or Employee object if you're seeing this error. Make sure your super() call aligns with one of the parent's existing constructors.
内容的提问来源于stack exchange,提问作者user9343206

