Java:继承类构造函数正在调用超类
在创建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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
超类没有默认构造函数。因此,您需要将适当的构造函数参数传递给超类:(
将其作为
Manager
和Engineer
构造函数中的顶行。)您还应该删除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:
(Put this as the top line in both the
Manager
andEngineer
constructors.) You should also remove thethis.emp_id = id
line, in both cases.In general, if your constructor doesn't start with a
super(...)
orthis(...)
statement (and you can only have one of these, not both), then it defaults to usingsuper()
(with no arguments).由于您已经指定了带参数的构造函数,因此 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.生成错误是因为您没有在 Employee 中定义默认构造函数(无参数)
,但有几点需要考虑:您正在通过构造函数设置
emp_id
并定义一个 getter 来读取它。看来这个领域本来就是私人的。否则直接访问即可。您已经在 Employee 中设置了一个构造函数来设置 ID,无需在同一个类中定义相同的构造函数。只需使用超类的构造函数即可。
在这种情况下,您不需要默认构造函数。
The error is generated since you didn't define a default constructor (no arguments) in Employee
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.
In this case you don't need the default constructor.
在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.