简体   繁体   English

Java编译器错误:“找不到符号构造函数..”?

[英]Java compiler error: “cannot find symbol constructor ..”?

i'm writing code for employee, manager, hourly worker for a class assignment but i've hit a problem that i can't figure out, the following is my code for employee followed by hourly worker. 我正在为班级分配的员工,经理,小时工编写代码,但是我遇到了一个我不知道的问题,以下是我为员工编写的代码,其次是小时工。 the problem is hourly worker won't compile, it's giving a "cannot find symbol constructor employee" error when i try to compile (employee class compiiles without issue. any suggestions please? i think i've been staring at it for so long i can no longer see the problem! thanks. pieter. 问题是小时工无法编译,当我尝试编译时,它给出“找不到符号构造函数的雇员”错误(员工类完全没有问题。请问任何建议吗?我想我一直盯着它看了很久我看不到问题了,谢谢。

EMPLOYEE CLASS - 员工班-

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

 }

HOURLY WORKER CLASS - 上班族-

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;
    }




}

You don't have a Employee constructor with two parameters: 您没有具有两个参数的Employee构造函数:

super(firstName, lastName);

Try using: 尝试使用:

super(firstName, lastName, 0.0);

EDIT as per tony request, here's a more detailed explanation. 根据Tony要求进行编辑 ,这是更详细的说明。

With super(firstName, lastName); 使用super(firstName, lastName); you're invoking ( trying to invoke ) a constructor in the class Employee which has two string parameters. 您正在调用(尝试调用) Employee类中的构造函数,该构造函数具有两个字符串参数。

Reviewing the Employee class definition, we see you don't have such constructor, but you have one with three parameters: 回顾Employee类的定义,我们看到您没有这样的构造函数,但是您有一个带有三个参数的构造函数:

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

So, the solution is to invoke that constructor instead. 因此,解决方案是改为调用该构造函数。 Since you don't have a default value for hourlyRate we can use 0.0 which is a double. 由于您没有hourlyRate的默认值, hourlyRate我们可以使用0.0,它是双hourlyRate值。

Other alternative would be to create a two parameter constructor in the Employee class 另一种选择是在Employee类中创建一个两个参数的构造函数

public Employee(String firstName, String lastName )

In the HourlyWorker constructor you tried to call the Employee constructor like this: 在HourlyWorker构造函数中,您尝试像这样调用Employee构造函数:

super(firstName, lastName);

but the Employee class doesn't have a constructor with two parameters. 但是Employee类没有带有两个参数的构造函数。 You need to pass a third parameter (hourly rate) like this: 您需要像这样传递第三个参数(每小时费率):

super(firstName, lastName, 42);

In HourlyWorker you call HourlyWorker您致电

super(firstName, lastName);

but the Employee constructor is 但是Employee构造函数是

Employee(String, String, double)

The signatures don't match. 签名不匹配。

EDIT: Incidentally, why does the HourlyWorker have a private wage member? 编辑:顺便说一句,为什么HourlyWorker有私人wage成员? How is it different (conceptually) to Employee.hourlyRate ? (与概念上)与Employee.hourlyRate有何不同?

在HourlyWorker类中,您正在使用两个参数调用Employee {super(firstname,lastname)}的构造函数,但是在Employee类中,您没有使用两个参数的任何构造函数。

您没有只接受两个参数的超级构造函数。

HourlyWorker的构造函数尝试调用super(firstName, lastName) ,但是在父类中没有声明这种构造函数。

In your HourlyWorker class, you have the following line of code: 在HourlyWorker类中,您具有以下代码行:

super(firstName, lastName);

But, there is no matching constructor in your employee class. 但是,您的员工类中没有匹配的构造函数。 Basically, the compiler is looking in your employee class for something like... 基本上,编译器正在您的员工类中寻找类似...

public Employee(String firstName, String lastName)
{
   ...
}

Define a new constructor, or call the constructor you've defined with the parameters you're missing. 定义一个新的构造函数,或使用缺少的参数调用您定义的构造函数。

You are calling HourlyEmployee's base class constructor (which is Employee's ctor) with 2 arguments instead of the 3 it wants. 您正在使用2个参数而不是它想要的3个参数来调用HourlyEmployee的基类构造函数(这是Employee的ctor)。

Change the line in HourlyEmployee ctor from: 将HourlyEmployee ctor中的行从以下位置更改:

super(firstName, lastName);

to

super(firstName, lastName, hourlyWage);

Also, if you are still wanting to have a constructor like 另外,如果您仍想拥有类似

public Employee(String firstName, String lastName) { ... }

and you know the default value from your double hourlyRate , you can try to write a new constructor like the next one: 并且您从double hourlyRate知道了默认值,可以尝试编写一个类似于下一个的新构造函数:

`public Employee(String firstName, String lastName) { `public Employee(String firstName,String lastName){

     Employee(firstName, lastName, 0.0);

}` }`

There's something fishy going on around the else if here. else if在这里, else if周围会发生一些可疑的else if It's repeated with another if and some curly braces missing. 重复此操作,并重复另一个if和一些花括号。 Indent your code properly and make sure the open curlys matches the closing ones. 正确缩进代码,并确保打开的卷发与关闭的卷发匹配。

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

Also, these kind of lines won't work as intended (you have three of them): 此外,这些行将无法按预期工作(您有三行):

   firstName = firstName;

It must be: 一定是:

   this.firstName = firstName;

Like you have in your second class. 就像您在第二节课上一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM