Java。隐式超级构造函数Employee()未定义。必须显式调用另一个构造函数 [英] Java. Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor

查看:4421
本文介绍了Java。隐式超级构造函数Employee()未定义。必须显式调用另一个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是Java的新人,我在我的生产工人类中得到这个错误。我的生产工人构造函数说明显式调用另一个构造函数。我不知道该怎么办。

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


推荐答案

构造函数为任何类,因为你知道创建一个对象。因此,构造函数应该包含其类的正确的初始化代码。但是如果你有一些类扩展另一个(让我们称之为父),那么类的构造函数不能包含定义初始化所需的所有代码(例如,你不能定义父类的私有字段)。这就是为什么类的构造函数必须调用其父的构造函数。如果你不明确地调用它,那么默认的父构造函数被调用(没有任何参数)。

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).

所以,在你的case,你可以实现默认构造函数父类或直接调用类中的任何构造函数。

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

这篇关于Java。隐式超级构造函数Employee()未定义。必须显式调用另一个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆