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

发布于 2024-12-02 21:21:10 字数 971 浏览 2 评论 0原文

在创建java程序时我遇到了一个问题,

子类构造函数通过调用超类的方法抛出错误

代码与此类似:

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

错误是这样的:

$ 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

为什么会发生这种情况?

While creating a java program i encountered a problem,

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 ?

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

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

发布评论

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

评论(4

も让我眼熟你 2024-12-09 21:21:10

超类没有默认构造函数。因此,您需要将适当的构造函数参数传递给超类:(

super(id);

将其作为 ManagerEngineer 构造函数中的顶行。)您还应该删除 this.emp_id = id 行,在这两种情况下。

一般来说,如果您的构造函数不以 super(...)this(...) 语句开头(并且您只能使用其中之一,不是两者),那么它默认使用 super() (不带参数)。

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.

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

分分钟 2024-12-09 21:21:10

由于您已经指定了带参数的构造函数,因此 Java 不提供不带参数的默认构造函数。您应该自己创建一个,或者通过使用 super(id) 作为扩展类构造函数中的第一行来显式调用您创建的构造函数。

Since you have specified a constructor with arguments, Java does not provide with a default constructor without arguments. 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.

鹤舞 2024-12-09 21:21:10

生成错误是因为您没有在 Employee 中定义默认构造函数(无参数)

class Employee {

    private int emp_id;

    public Employee() {
    }

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

    public int get_id() {
            return emp_id;
    }

}

,但有几点需要考虑:您正在通过构造函数设置 emp_id 并定义一个 getter 来读取它。看来这个领域本来就是私人的。否则直接访问即可。

您已经在 Employee 中设置了一个构造函数来设置 ID,无需在同一个类中定义相同的构造函数。只需使用超类的构造函数即可。

class Manager extends Employee {

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

}

在这种情况下,您不需要默认构造函数。

The error is generated since you didn't define a default constructor (no arguments) in 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. 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. 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.

黯淡〆 2024-12-09 21:21:10

在java中,子类构造函数总是调用其父类的构造函数之一。这是正确初始化类所必需的。即使它是子类化的,也必须设置字段和状态,这就是 java 中的完成方式。如果没有明确指定,它将调用默认的无参数构造函数。

In java, a sub class constructor always calls one of its parent class's constructor. 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. If none is explicitly specified, it is calling the default no-arg constructor.

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