简体   繁体   English

Java:继承的类构造函数正在调用Super类

[英]Java: Inherited class constructor is calling Super class

While creating a java program i encountered a problem, 在创建Java程序时,我遇到了一个问题,

A subclass constructor is throwing an Error by calling the Superclass's method 子类构造函数通过调用超类的方法抛出错误

The code is similar to this : 代码类似于以下内容:

class Manage
{
    public static void main(String[] args) 
    {
        Manager m1 = new Manager ( 35 );
    }
}

class Employee
{
        int emp_id;
        public Employee(int id)
        {
                this.emp_id = id;
        }
        public int get_id()
        {
                return emp_id;
        }

}
class Manager extends Employee
{
        public Manager(int id )
        {
                this.emp_id = id ;
        }
}

class Engineer extends Employee
{
        public Engineer(int id)
        {
                this.emp_id = id ;
        }
}

And the error is something like this : 错误是这样的:

$ javac app.java 
app.java:25: cannot find symbol
symbol  : constructor Employee()
location: class Employee
        {
        ^
app.java:33: cannot find symbol
symbol  : constructor Employee()
location: class Employee
        {
        ^
2 errors

Why does this happen ? 为什么会这样?

The superclass doesn't have a default constructor. 超类没有默认的构造函数。 So you need to pass the appropriate constructor arguments to the superclass: 因此,您需要将适当的构造函数参数传递给超类:

super(id);

(Put this as the top line in both the Manager and Engineer constructors.) You should also remove the this.emp_id = id line, in both cases. (在ManagerEngineer构造函数中,将其作为第一行。)在两种情况下,您还都应删除this.emp_id = id行。

In general, if your constructor doesn't start with a super(...) or this(...) statement (and you can only have one of these, not both), then it defaults to using super() (with no arguments). 通常,如果您的构造函数不是以super(...)this(...)语句开头(并且您只能使用其中之一,不能同时使用两者),则默认使用super() (带有没有参数)。

Since you have specified a constructor with arguments, Java does not provide with a default constructor without arguments. 由于您已经指定了带有参数的构造函数,因此Java不会提供不带参数的默认构造函数。 You should create one yourself, or explicitly call the constructor you have created, by using super(id) as first line in the extended classes constructors. 您应该自己创建一个,或者通过使用super(id)作为扩展类构造函数的第一行来显式调用您创建的构造函数。

The error is generated since you didn't define a default constructor (no arguments) in Employee 由于未在Employee中定义默认构造函数(无参数),因此产生了错误

class Employee {

    private int emp_id;

    public Employee() {
    }

    public Employee(int id)  {
            this.emp_id = id;
    }

    public int get_id() {
            return emp_id;
    }

}

but there are a couple of points to consider: you are setting emp_id via the constructor and defined a getter to read it. 但是有两点需要考虑:您正在通过构造函数设置emp_id并定义了一个读取方法。 It seems that the field was meant to be private. 似乎该领域是私有的。 Otherwise you can just access directly. 否则,您可以直接访问。

You already have a constructor in Employee setting the ID no need to define the same constructor in the same class. 您已经在Employee中设置了ID的构造函数,而无需在同一类中定义相同的构造函数。 Just use the constructor of the superclass. 只需使用超类的构造函数即可。

class Manager extends Employee {

    public Manager(int id ) {
        super(id);  // calls the superclass constructor
    }

}

In this case you don't need the default constructor. 在这种情况下,您不需要默认的构造函数。

In java, a sub class constructor always calls one of its parent class's constructor. 在Java中,子类构造函数始终调用其父类的构造函数之一。 This is neccessary for the class to be initialized properly. 这对于正确初始化该类是必要的。 Even when it is subclassed, the fields and state must be setup and this is how its done in java. 即使是子类,也必须设置字段和状态,这就是在Java中的工作方式。 If none is explicitly specified, it is calling the default no-arg constructor. 如果没有明确指定,则它正在调用默认的no-arg构造函数。

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

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