简体   繁体   English

在Java中,您可以通过父类的子类的构造函数调用父类的超类构造函数吗?

[英]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.

相关问题 Java:子类构造函数类如何中和父构造函数? - Java: How Can be child class constructor class neutralize parent constructor? Java-您可以从超类继承并拥有一个静态构造函数来引用基类而不是父类吗? - Java - Can you inherit from a superclass and have a static constructor that references the base classes as opposed to the parent class? 在Java中的子类中使用父构造函数 - Using parent constructor in a child class in Java 为什么对父 class 构造函数的调用不调用在子 class 中覆盖的父方法? - Why does the call to a parent class constructor not call the parent's methods that are overriden in the child class? 有没有一种方法可以调用一个父类构造函数,该构造函数从一个不具有自己构造函数的子类中获取参数? - Is there a way I can call a parent class constructor that takes parameters from a child class that does not have a constructor of it's own? 如何隐含地调用父类的构造函数 - How to implictly call parent class's constructor 如何调用活动的父类的构造函数 - How to call the constructor of an activity's parent class 如何避免在Java中隐式调用父类的构造函数? - How to avoid implicit call to parent class's constructor in Java? PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? Java父级通过子级构造函数中的Child类Parent属性私有集实例化 - Java Parent instantiated through Child class Parent attribute private set in child constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM