gpt4 book ai didi

Java编译器错误: "cannot find symbol constructor .."?

转载 作者:行者123 更新时间:2023-12-01 06:26:31 33 4
gpt4 key购买 nike

我正在为类(class)作业编写员工、经理、小时工的代码,但遇到了一个我无法解决的问题,以下是我为员工编写的代码,然后是小时工。问题是小时工无法编译,当我尝试编译时,它给出了“找不到符号构造函数员工”错误(员工类编译没有问题。请问有什么建议吗?我想我已经盯着它看了很久了再也看不到问题了!谢谢。pieter。

员工类别 -

public class Employee
{
public String firstName;
public String lastName;
public double hourlyRate;
final static double NORMAL_WORKWEEK = 37.5;

public Employee(String firstName, String lastName, double hourlyRate)
{
setFirstName(firstName);
setLastName(lastName);
setHourlyRate(hourlyRate);
}
//Accessor and Mutator Methods for the employee's first name.
public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
firstName = firstName;
}
//Accessor and Mutator Methods for the employee's last name.
public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
lastName = lastName;
}
//Access and Mutator Methods for the employee's hourly rate.
public double getHourlyRate()
{
return hourlyRate;
}
public void setHourlyRate(double hourlyRate)
{
//If the user input is valid, update the employee's hour rate with the newly input value.

if(hourlyRate > 0)
{
hourlyRate = hourlyRate;
}
//Otherwise prevent an hour rate greater than zero being overwritten
else if(hourlyRate <=0)
{

if(hourlyRate <= 0)
{
hourlyRate = 0;
}

decorateConsole();

//Alert the user to their mistake.
System.out.println("Error ! ! ! - An attempt to set the employee " + this.firstName + " " + this.lastName + "'s hourly rate to zero was detected.\n");

decorateConsole();

}


}

public void printState()
{

decorateConsole();

System.out.println("[FIRST NAME] = " + firstName + " [LAST NAME] = " + lastName + " [HOURLY RATE] = " + hourlyRate + "\n");

decorateConsole();

}

public void decorateConsole()
{

System.out.println("+-< EMPLOYEE INFO >-------------------------------------------------------------------------------------------------------------------------+\n");

}

小时工类 -

public class HourlyWorker extends Employee
{
private double wage;
private double hours;

public HourlyWorker(String firstName, String lastName, double hourlyWage, double hoursWorked)
{
super(firstName, lastName);
this.wage = wage;
this.hours = hours;
}

public void setWage (double hourlyWage)
{
this.wage = wage;
}
public void getWage()
{
return wage;
}
public void setHours (double hours)
{
this.hours = hours;
}
public double getHours()
{
return hours;
}




}

最佳答案

您没有带有两个参数的 Employee 构造函数:

super(firstName, lastName);

尝试使用:

super(firstName, lastName, 0.0);

编辑根据托尼的要求,这里有更详细的解释。

使用 super(firstName, lastName);,您将调用(尝试调用)Employee 类中的构造函数,该构造函数具有两个字符串参数。

查看 Employee 类定义,我们发现您没有这样的构造函数,但有一个带有三个参数的构造函数:

public Employee(String firstName, String lastName, double hourlyRate)

因此,解决方案是调用该构造函数。由于您没有 hourlyRate 的默认值,我们可以使用 0.0,它是一个 double 值。

另一种选择是在 Employee 类中创建一个两个参数的构造函数

public Employee(String firstName, String lastName )

关于Java编译器错误: "cannot find symbol constructor .."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318984/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com