简体   繁体   English

子类中的Java构造函数错误

[英]Java constructor error in child class

class Person 
{
    String name = "No name";

    public Person(String nm)
    {
        name = nm;
    }
}
class Employee1 extends Person
{
    String empID ="0000";
    public Employee1(String id)
    {                            // Line 1
        empID = id;
    }
}
public class EmployeeTest 
   {
       public static void main(String[] args) {
           Employee1 e = new Employee1("4321");
           Person p = new Person("Hello");
           System.out.println(e.empID);
       }
   }

I get compilation error saying constructor Person in class Person cannot be applied to given types; required String found no arguments 我得到编译错误说constructor Person in class Person cannot be applied to given types; required String found no arguments constructor Person in class Person cannot be applied to given types; required String found no arguments but I am passing arguments for both the parent and the child class when I create new object in main method. constructor Person in class Person cannot be applied to given types; required String found no arguments但是当我在main方法中创建新对象时,我传递了父类和子类的参数。 Unable to figure out why compilation error? 无法弄清楚为什么编译错误?

You need to properly create the parent class, passing it a name as the Person construtor requires: 您需要正确创建父类,并按照Person construtor的要求传递一个name

public Employee1(String id) {
    super("person name");   // create the parent
    empID = id;
}

Or maybe something like: 或者类似的东西:

public Employee1(String id, String name) {
    super(name);   // create the parent
    empID = id;
}

public static void main(String[] args) {
    Employee1 e = new Employee1("4321", "Hello");
    // ...
}

Because constructor in child class implicitly calls the parameter-less constructor of it's immediate super class only if the default constructor does not explicitly call a superclass constructor so 因为只有当默认构造函数没有显式调用超类构造函数时,子类中的构造函数才会隐式调用它的直接超类的无参数构造函数

public Employee1(String id)
{                            // Line 1
    empID = id;
}

will try to call constructor in super class as you have not called it explicitly so you can say your code will be like this 将尝试在超类中调用构造函数,因为您没有明确调用它,因此您可以说您的代码将是这样的

public Employee1(String id)
{
    super();                            // Line 1
    empID = id;
}

but in you parent class there is no "no argument" constructor, so it is giving error like this. 但在你的父类中没有“无参数”构造函数,所以它给出了这样的错误。

On the constructor of employee you must call first the constructor of person as in super(name); employee的构造函数中,您必须首先调用person的构造函数super(name); and then initialize the subclass 然后初始化子类

class Employee1 extends Person
{
    String empID ="0000";
    public Employee1(String id, String name)
    {                            // Line 1
        super(name);
        empID = id;
    }
}

The implicit super constructor Person() is undefined. 隐式超级构造函数Person()未定义。

So you must explicitly invoke its parent constructor. 因此,您必须显式调用其父构造函数。

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

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