为什么superly()在HourlyEmployee类中没有正常工作?

时间:2018-02-12 01:34:19

标签: java super

我正在尝试编写一个简单的工资单系统程序。我在super(employee_1);方法中遇到HourlyEmployee(HourlyEmployee employee_1)问题。我得到的原因是“实际和正式的论点长度不同。但是,当我更改参数以包含所有内容时,我在行main class(payrollSystem_1)的{​​{1}}中收到错误,它表示方法应该只说HourlyEmployee employee_2 = new HourlyEmployee(employee_1);

我只是陷入困境,无法修复HourlyEmployee(HourlyEmployee employee_1)

PayrollSystem_1.java

super(employee_1)

HourlyEmployee.java

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()) );

Employee.java

        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;

            }

        }

1 个答案:

答案 0 :(得分:0)

当你调用super时,你的构造函数会出现问题 - super没有这样的构造函数

        public HourlyEmployee(HourlyEmployee employee_1) {
            super(employee_1);
            this.hourlyRate = employee_1.hourlyRate;
            this.periodHours = employee_1.periodHours;
        }

像这样更改构造函数:

    public HourlyEmployee(HourlyEmployee employee_1) {
        super(employee_1.employeeID, employee_1.firstName, employee_1.lastName, employee_1.listOfPaychecks);
        this.hourlyRate = employee_1.hourlyRate;
        this.periodHours = employee_1.periodHours;
    }

或者更好的是:

    public HourlyEmployee(HourlyEmployee employee_1) {
        this (employee_1.employeeID, employee_1.firstName, employee_1.lastName, employee_1.listOfPaychecks, employee_1.hourlyRate, employee_1.periodHours);
    }