了解 Java super() 构造函数
Understanding Java super() constructor
我正在尝试理解 Java super()
构造函数。下面一起来看看class:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
}
这个class会编译。如果我们创建一个新的 Point
对象说 Point a = new Point();
将调用不带参数的构造函数:Point()
.
如果我错了请纠正我,在执行 this(0,0)
之前,将调用 Class
构造函数,然后才调用 Point(0,0)
。
如果这是真的,那么说默认调用 super()
是否正确?
现在让我们看一下稍作改动的相同代码:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
super(); // this is the change.
this(0, 0);
}
}
现在,代码无法编译,因为 this(0,0)
不在构造函数的第一行。这就是我感到困惑的地方。为什么代码无法编译? super()
不会被调用吗? (Class 构造函数如上所述)。
通过调用 this(...)
,您将被迫在您正在调用的构造函数中调用超级构造函数(如果存在)。
调用 this(...)
或 super(...)
始终是您在构造函数中调用的第一个方法。
编辑
class Point {
private int x, y;
public Point(int x, int y) {
super(...); //would work here
this.x = x;
this.y = y;
}
public Point() {
this(0, 0); //but this(...) must be first in a constructor if you want to call another constructor
}
}
this(0, 0);
会调用,同一个class和super()
的构造函数会调用Point
[=28=的super/parentclassclass ].
Now, the code won't compile because this(0,0) is not in the first line
of the constructor. This is where I get confused. Why the code won't
compile? Doesn't super() is being called anyway?
super()
必须是构造函数的第一行并且 this()
必须是构造函数的第一行。所以两者都不能在第一行(不可能),也就是说,我们不能在构造函数中同时添加。
关于您的代码的解释:
this(0, 0);
将调用带有两个参数的构造函数(在 Point
class 中)并且两个参数构造函数将隐式调用 super()
(因为你没有明确调用它)。
将 super() 放入您的第一个构造函数中,它应该可以工作。
class Point {
private int x, y;
public Point(int x, int y) {
super(); // this is the change.
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
}
您可以调用 this()
或 super()
但不能从同一个构造函数中同时调用。当您调用 this()
时,super()
会从 other 构造函数(您使用 this()
调用的构造函数)自动调用。
构造函数可以使用 super()
方法调用来调用超级 class 的构造函数。唯一的限制是它应该是第一个语句。
public Animal() {
super();
this.name = "Default Name";
}
这段代码可以编译吗?
public Animal() {
this.name = "Default Name";
super();
}
答案是否。 super()
应始终在构造函数的第一行调用。
我正在尝试理解 Java super()
构造函数。下面一起来看看class:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
}
这个class会编译。如果我们创建一个新的 Point
对象说 Point a = new Point();
将调用不带参数的构造函数:Point()
.
如果我错了请纠正我,在执行 this(0,0)
之前,将调用 Class
构造函数,然后才调用 Point(0,0)
。
如果这是真的,那么说默认调用 super()
是否正确?
现在让我们看一下稍作改动的相同代码:
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
super(); // this is the change.
this(0, 0);
}
}
现在,代码无法编译,因为 this(0,0)
不在构造函数的第一行。这就是我感到困惑的地方。为什么代码无法编译? super()
不会被调用吗? (Class 构造函数如上所述)。
通过调用 this(...)
,您将被迫在您正在调用的构造函数中调用超级构造函数(如果存在)。
调用 this(...)
或 super(...)
始终是您在构造函数中调用的第一个方法。
编辑
class Point {
private int x, y;
public Point(int x, int y) {
super(...); //would work here
this.x = x;
this.y = y;
}
public Point() {
this(0, 0); //but this(...) must be first in a constructor if you want to call another constructor
}
}
this(0, 0);
会调用,同一个class和super()
的构造函数会调用Point
[=28=的super/parentclassclass ].
Now, the code won't compile because this(0,0) is not in the first line of the constructor. This is where I get confused. Why the code won't compile? Doesn't super() is being called anyway?
super()
必须是构造函数的第一行并且 this()
必须是构造函数的第一行。所以两者都不能在第一行(不可能),也就是说,我们不能在构造函数中同时添加。
关于您的代码的解释:
this(0, 0);
将调用带有两个参数的构造函数(在 Point
class 中)并且两个参数构造函数将隐式调用 super()
(因为你没有明确调用它)。
将 super() 放入您的第一个构造函数中,它应该可以工作。
class Point {
private int x, y;
public Point(int x, int y) {
super(); // this is the change.
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
}
您可以调用 this()
或 super()
但不能从同一个构造函数中同时调用。当您调用 this()
时,super()
会从 other 构造函数(您使用 this()
调用的构造函数)自动调用。
构造函数可以使用 super()
方法调用来调用超级 class 的构造函数。唯一的限制是它应该是第一个语句。
public Animal() {
super();
this.name = "Default Name";
}
这段代码可以编译吗?
public Animal() {
this.name = "Default Name";
super();
}
答案是否。 super()
应始终在构造函数的第一行调用。