爪哇。隐式超级构造函数 Employee() 未定义。必须显式调用另一个构造函数

发布于 2024-10-28 15:59:03 字数 1580 浏览 6 评论 0原文

您好,我是 Java 新手,我在生产工作线程类中遇到此错误。我的生产工作线程构造函数显示显式调用另一个构造函数。我不知道该怎么办?

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}



public class ProductionWorker extends Employee
{
      private int shift;
      private double hourlyrate;
       // error is here (Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor).
      public ProductionWorker(int shift, double hourlyrate)
      {
            setShift(shift);
            setHourlyPayRate(hourlyrate);
      }

      public void setShift(int s)
      {
            shift = s;
      }
      public void setHourlyPayRate(double rate)
      {
            hourlyrate = rate;
      }

      public int getShift()
      {
            return shift;
      }
      public double getHourlyPayRate()
      {
            return hourlyrate;
      }
}

Hello I'm new to Java, I'm getting this error in my production worker class. My Production worker constructor says explicitly invoke another constructor. I don't know what to do?.

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}



public class ProductionWorker extends Employee
{
      private int shift;
      private double hourlyrate;
       // error is here (Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor).
      public ProductionWorker(int shift, double hourlyrate)
      {
            setShift(shift);
            setHourlyPayRate(hourlyrate);
      }

      public void setShift(int s)
      {
            shift = s;
      }
      public void setHourlyPayRate(double rate)
      {
            hourlyrate = rate;
      }

      public int getShift()
      {
            return shift;
      }
      public double getHourlyPayRate()
      {
            return hourlyrate;
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

滿滿的愛 2024-11-04 15:59:03

正如您所知,任何类的任何构造函数都会创建一个对象。因此,构造函数应该包含其类的正确初始化代码。但是,如果您有某个类扩展了另一个类(我们称其为“父类”),则该类的构造函数不能包含定义初始化所需的所有代码(例如,您不能定义父类的私有字段)。这就是为什么该类的构造函数必须调用其父类的构造函数。如果您没有显式调用它,则调用默认的父构造函数(不带任何参数)。

因此,在您的情况下,您可以在父级中实现默认构造函数,也可以直接调用类中的任何构造函数。

Any constructor for any class as you know creates an object. So, the constructor should contain proper initialization code for its class. But if you have some class which extends another one (lets call it "parent") then constructor for the class cannot contain all the code needed for the initialization by definition (for example, you cannot define private fields of the parent). That's why constructor of the class has to call constructor of its parent. If you do not call it explicitly then the default parent constructor is called (which is without any parameter).

So, in your case, you can either implement default constructor in parent or directly call any constructor in the class.

凌乱心跳 2024-11-04 15:59:03

正如其他人已经提到的,您需要在 Employee 类中提供默认构造函数 public Employee(){}

发生的情况是,编译器自动为任何没有构造函数的类提供无参数的默认构造函数。如果您的类没有显式的超类,那么它就有一个Object的隐式超类,它有一个无参构造函数。在这种情况下,您在 Employee 类中声明一个构造函数,因此您必须还提供无参构造函数。

话虽如此,Employee 类应该如下所示:

Your class Employee

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(){} // No-argument Constructor

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}

这里是 Java Oracle 教程 - 为您的类提供构造函数章节。通过它,您将对正在发生的事情有更清晰的了解。

As others have already mentioned you are required to provide a default constructor public Employee(){} in your Employee class.

What happens is that the compiler automatically provides a no-argument, default constructor for any class without constructors. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor. In this case you are declaring a constructor in your class Employee therefore you must provide also the no-argument constructor.

Having said that Employee class should look like this:

Your class Employee

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(){} // No-argument Constructor

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}

Here is the Java Oracle tutorial - Providing Constructors for Your Classes chapter. Go through it and you will have a clearer idea of what is going on.

草莓酥 2024-11-04 15:59:03

ProductionWorker 扩展了 Employee,因此可以说它具有 Employee 的所有功能。为了实现这一点,Java 自动在每个构造函数的第一行中放置一个 super(); 调用,您可以手动放置它,但通常没有必要。在您的情况下,这是必要的,因为由于 Employee 的构造函数具有参数,因此无法自动调用 super();

您需要在 Employee 类中定义默认构造函数,或者在构造函数的第一行调用 super('Erkan', 21, new Date());在 ProductionWorker 中。

ProductionWorker extends Employee, thus it is said that it has all the capabilities of an Employee. In order to accomplish that, Java automatically puts a super(); call in each constructor's first line, you can put it manually but usually it is not necessary. In your case, it is necessary because the call to super(); cannot be placed automatically due to the fact that Employee's constructor has parameters.

You either need to define a default constructor in your Employee class, or call super('Erkan', 21, new Date()); in the first line of the constructor in ProductionWorker.

独守阴晴ぅ圆缺 2024-11-04 15:59:03

每当父类缺少无参构造函数时,都需要显式调用父类构造函数。您可以向父类添加无参构造函数,也可以在子类中显式调用父类构造函数。

An explicit call to a parent class constructor is required any time the parent class lacks a no-argument constructor. You can either add a no-argument constructor to the parent class or explicitly call the parent class constructor in your child class.

烟沫凡尘 2024-11-04 15:59:03

当您没有让构造函数立即调用 super 时,也会出现此问题。

所以这会起作用:

  public Employee(String name, String number, Date date)
  {
    super(....)
  }

但这不会:

  public Employee(String name, String number, Date date)
  {
    // an example of *any* code running before you call super.
    if (number < 5)
    {
       number++;
    }

    super(....)
  }

第二个例子失败的原因是因为java试图隐式地调用

super(name,number,date)

你的构造函数中的第一行......所以java看不到你已经调用了super 稍后将在构造函数中进行。它本质上试图做到这一点:

  public Employee(String name, String number, Date date)
  {
    super(name, number, date);

    if (number < 5)
    {
       number++;
    }

    super(....)
  }

所以解决方案非常简单...只是不要在您的 super 调用之前放置代码;-) 如果您需要在调用 super 之前初始化某些内容,请执行此操作在另一个构造函数中,然后调用旧的构造函数...就像这个示例从这个 StackOverflow 帖子中提取

public class Foo
{
    private int x;

    public Foo()
    {
        this(1);
    }

    public Foo(int x)
    {
        this.x = x;
    }
}

This problem can also come up when you don't have your constructor immediately call super.

So this will work:

  public Employee(String name, String number, Date date)
  {
    super(....)
  }

But this won't:

  public Employee(String name, String number, Date date)
  {
    // an example of *any* code running before you call super.
    if (number < 5)
    {
       number++;
    }

    super(....)
  }

The reason the 2nd example fails is because java is trying to implicitely call

super(name,number,date)

as the first line in your constructor.... So java doesn't see that you've got a call to super going on later in the constructor. It essentially tries to do this:

  public Employee(String name, String number, Date date)
  {
    super(name, number, date);

    if (number < 5)
    {
       number++;
    }

    super(....)
  }

So the solution is pretty easy... Just don't put code before your super call ;-) If you need to initialize something before the call to super, do it in another constructor, and then call the old constructor... Like in this example pulled from this StackOverflow post:

public class Foo
{
    private int x;

    public Foo()
    {
        this(1);
    }

    public Foo(int x)
    {
        this.x = x;
    }
}
痴意少年 2024-11-04 15:59:03

最近我的计算机实验室遇到了这个问题。很简单,Erkan 回答正确。只需将 super("您的子类的名称") 与您的问题相关 --> super("ProductionWorker); 作为 subclass' 构造函数的第一行。

Had this problem recently in my comp lab. It's simple and Erkan answered it correctly. Just put super("the name of your subclass") So in relation to your problem --> super("ProductionWorker); as the first line of your subclass' constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文