java为什么super()在HourlyEmployee类中不能正常工作?
我想写一个简单的工资系统程序。我对HourlyEmployee(HourlyEmployee employee_1)
方法中的super(employee_1);
有问题。我得到的原因是“实际参数和形式参数的长度不同。然而,当我更改参数以包含所有内容时,我在main class(payrollSystem_1)
行中得到一个错误,它说该方法应该只说HourlyEmployee(HourlyEmployee employee_1)
我只是卡住了,修不好super(employee_1)
工资制度1。爪哇
package payrollsystem_1;
import java.util.*;
public class PayrollSystem_1 {
public static void main(String[] args) {
// TODO code application logic here
// Creating an HourlyEmployee object using the first constructor
HourlyEmployee employee_1 = new HourlyEmployee(1, "Janette", "Hernandez", null, 14.75, 30);
// Creating an HourlyEmployee object using the copy constructor
HourlyEmployee employee_2 = new HourlyEmployee(employee_1);
// Calling some of the setter methods in the HourlyEmployee class.
employee_2.setEmployeeID(2);
employee_2.setFirstName("Marcela");
employee_2.setLastName("Brown");
employee_2.setHourlyRate(25);
employee_2.setPeriodHours(40);
System.out.println("\nCalling some of the getter methods in the HourlyEmployee class...");
System.out.println( String.format("%-30s%s", "Employee ID:", employee_1.getEmployeeID()) );
System.out.println( String.format("%-30s%s", "First Name:", employee_1.getFirstName()) );
System.out.println( String.format("%-30s%s", "Last Name:", employee_1.getLastName()) );
System.out.println( String.format("%-30s%s", "Hourly Rate:", employee_1.getHourlyRate()) );
System.out.println( String.format("%-30s%s", "Period Hours:", employee_1.getPeriodHours()) );
小时雇员。爪哇
public class HourlyEmployee extends Employee {
private double hourlyRate;
private double periodHours;
/**
* @param employeeID
* @param firstName
* @param lastName
* @param listOfPaychecks
* @param hourlyRate
* @param periodHours
* Constructor with parameters
*/
public HourlyEmployee( int employeeID, String firstName, String lastName, ArrayList<Paycheck> listOfPaychecks,
double hourlyRate, double periodHours) {
super(employeeID, firstName, lastName, listOfPaychecks);
this.hourlyRate = hourlyRate;
this.periodHours = periodHours;
}
/**
* @param employee_1
*
*
*/
public HourlyEmployee(HourlyEmployee employee_1) {
super(employee_1);
this.hourlyRate = employee_1.hourlyRate;
this.periodHours = employee_1.periodHours;
}
/**
* @return hourlyRate
*/
public double getHourlyRate() {
return hourlyRate;
}
/**
* @param hourlyRate
*/
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
/**
* @return periodHours
*/
public double getPeriodHours() {
return periodHours;
}
/**
* @param periodHours
*/
public void setPeriodHours(double periodHours) {
this.periodHours = periodHours;
}
}
雇员。爪哇
public abstract class Employee {
int employeeID;
String firstName;
String lastName;
ArrayList<Paycheck>listOfPaychecks;
double hourlyRate;
double periodHours;
/**
* @param employeeID
* @param firstName
* @param lastName
* @param listOfPaychecks
* Parameterized Constructor
*/
public Employee (int employeeID, String firstName, String lastName,
ArrayList<Paycheck> listOfPaychecks){
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.listOfPaychecks = listOfPaychecks;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID){
this.employeeID = employeeID;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public ArrayList<Paycheck> getListOfPaychecks(){
return listOfPaychecks;
}
public void setListOfPaychecks(ArrayList<Paycheck> listOfPaychecks){
this.listOfPaychecks = listOfPaychecks;
}
@Override
public String toString() {
StringBuffer employeeInfo=new StringBuffer();
employeeInfo.append("Employee ID: "+employeeID+" First Name: "+firstName+" Last Name: "+lastName+"\nPay Check Details: \n");
for (Paycheck paycheck : listOfPaychecks) {
employeeInfo.append(paycheck.toString());
}
return employeeInfo.toString();
}
}
# 1 楼答案
当你调用super时,问题在于你的构造函数——super没有这样的构造函数
像这样更改构造函数:
或者最好还是这样: