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

查看:19
本文介绍了爪哇.隐式超级构造函数 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).

因此,就您而言,您可以在父级中实现默认构造函数,也可以直接调用类中的任何构造函数.

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

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

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