[英]In Java, can you call a parent class's superclass constructor through the constructor of the child class of the parent class?
I know the wording is a bit confusing and strange, but bear with me, I have no clue how to phrase that any shorter. 我知道措辞有点令人困惑和奇怪,但忍受我,我不知道如何短语,任何更短。
Say you have a class called SuperBlah
, and you inherited it in a class called Blah
then you inherited Blah
into a class called ChildBlah
(So SuperBlah-Blah-ChildBlah
). 假设你有一个叫做类
SuperBlah
,你继承了它在一个名为类Blah
,你继承Blah
到一个名为类ChildBlah
(所以SuperBlah-Blah-ChildBlah
)。 Would the super();
会
super();
keyword used in the ChildBlah
's constructor called the SuperBlah
's constructor if Blah
doesn't have a constructor? 如果
Blah
没有构造函数,在ChildBlah
的构造函数中使用的关键字称为SuperBlah
的构造函数?
To those of you who said no, then why does this work? 对于那些拒绝的人,那为什么会这样呢? We have a class called BlusterBug that extends Critter class, and calls the super in the BlusterBug's constructor.
我们有一个名为BlusterBug的类,它扩展了Critter类,并在BlusterBug的构造函数中调用了super。 Critter doesn't have a constructor, but the Class that Critter extends does have a constructor.
Critter没有构造函数,但是Critter扩展的Class确实有一个构造函数。 (I purposefully omitted the rest of the code on the classes)
(我故意省略了类的其余代码)
public class BlusterCritter extends Critter {
// instance vaiables
private int courageFactor;
private static final double DARKENING_FACTOR = 0.05;
// create a constructor(include what is necesary for parent)
public BlusterCritter(int c)
{
super();
courageFactor = c;
}
Then in the Critter class, it doesn't have any constructors! 然后在Critter类中,它没有任何构造函数!
public class Critter extends Actor// omitted all the code
{
/**
* A critter acts by getting a list of other actors, processing that list,
* getting locations to move to, selecting one of them, and moving to the
* selected location.
*/
public void act()
{
if (getGrid() == null)
return;
ArrayList<Actor> actors = getActors();
processActors(actors);
ArrayList<Location> moveLocs = getMoveLocations();
Location loc = selectMoveLocation(moveLocs);
makeMove(loc);
}
But then the Actor class has a constructor! 但是Actor类有一个构造函数!
public class Actor
{
private Grid<Actor> grid;
private Location location;
private int direction;
private Color color;
/**
* Constructs a blue actor that is facing north.
*/
public Actor()
{
color = Color.BLUE;
direction = Location.NORTH;
grid = null;
location = null;
}
And the weirdest part? 最奇怪的部分? The program works perfectly fine and the compiler doesn't do catch any errors!
该程序工作得很好,编译器不会捕获任何错误! How is this happening?
这是怎么回事?
If Blah
doesn't have any constructor, the compiler will generate an implicit no-argument constructor for Blah
that is just super()
--that is, it will call SuperBlah
's constructor. 如果
Blah
没有任何构造函数,编译器将为Blah
生成一个隐式的无参数构造函数,它只是super()
- 也就是说,它将调用SuperBlah
的构造函数。
So when ChildBlah
's constructor calls super()
, it will call this implicit constructor that the compiler generated for Blah
, which results in SuperBlah
's constructor being called. 因此,当
ChildBlah
的构造函数调用super()
,它将调用编译器为Blah
生成的隐式构造函数,这会导致调用SuperBlah
的构造函数。 So in some sense, I think the answer to your question is "yes". 所以在某种意义上,我认为你的问题的答案是肯定的。
But this only works if Blah
has no constructors. 但这只有在
Blah
没有构造函数的情况下才有效。 (And in your actual code, Critter
doesn't have any constructors, so it works.) If any other constructors are defined for Blah
, but there's no accessible no-argument constructor, then there won't be an implicit no-argument constructor, and the result will be an error at compile time. (在你的实际代码中,
Critter
没有任何构造函数,所以它有效。)如果为Blah
定义了任何其他构造函数,但是没有可访问的无参数构造函数,那么就不会有隐式的无参数构造函数,结果将是编译时的错误。
No, it won't. 不,它不会。 It will call the implicit default constructor in
Blah
. 它将在
Blah
调用隐式默认构造函数。
If Blah
defines other constructors (with parameters) but has no 如果
Blah
定义了其他构造函数(带参数)但没有
default constructor, then this will generate a compilation error. 默认构造函数,然后这将生成编译错误。
Also, note that constructors are not inherited 另请注意,构造函数不是继承的
(if that relates in any way to your question/thoughts). (如果这与您的问题/想法有任何关系)。
Yes it will. 是的,它会的。 If
Blah
doesn't have any constructor, it will call default empty constructor and transitively it will call non-parametric SuperBlah
constructor. 如果
Blah
没有任何构造函数,它将调用默认的空构造函数,并且传递它将调用非参数的SuperBlah
构造函数。
它将调用Blah的构造函数,如果Blah没有编译器错误则抛出编译器错误。
No. Constructors aren't like methods in the respect of optional overriding by an intermediate class in the hierarchy. 不可以。构造函数与层次结构中的中间类可选覆盖方面的方法不同。 If an instance method of the super class is not implemented in the intermediate class, calling the method from the subclass via
super.method()
will invoke the superclass method. 如果超类的实例方法没有在中间类中实现,则通过
super.method()
从子类调用该方法将调用超类方法。 But a constructor for every class in the hierarchy must be called - you can't skip over the intermediate class's constructors. 但是必须调用层次结构中每个类的构造函数 - 您不能跳过中间类的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.