简体   繁体   English

Java继承概念引用超类构造函数?

[英]Java Inheritance Concept referring to super class constructor?

I was doing a simple Inheritance program, However I got confused while doing this program. 我正在做一个简单的继承程序,但是在做这个程序时我感到很困惑。

Here is the simple program: 这是一个简单的程序:

class Box {
    double width;
    double height;
    double depth;
    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }
    //Error on removing the Box() Constructor
    Box() {
        width = -1; 
        height = -1;   
        depth = -1;  
    }

    double volume() {
        return width * height * depth;
    }
}

class BoxWeight extends Box {
    double weight,h,w,d; 

    BoxWeight(double w, double h, double d, double m) {

        // If i add super(w,h,d) here, then it is working, WHy?
        this.h = h;
        this.d = d;
        this.w = w;
        weight = m;
    }
}
class DemoBoxWeight {
    public static void main(String args[]) {
        //Implementation of above
    }
}

What confused me is, When i remove the Box() constructor, It gives an error to me on line where BoxWeight(double w, double h, double d, double m) constructor is defined. 令我感到困惑的是,当我删除Box()构造函数时,它给我一个错误,我在线上BoxWeight(double w, double h, double d, double m)构造函数。 The error is: "constructor Box in class Box cannot be applied to given types".

If i call super() from within the BoxWeight() , then it works fine. 如果我从BoxWeight()调用super() BoxWeight() ,那么它可以正常工作。 I don't understand why?? 我不明白为什么? I know the concept is pretty simple, but I am having a hard time understanding it. 我知道这个概念非常简单,但我很难理解它。 Any Help is Appreciated. 任何帮助都会得到赞赏。

Your constructor BoxWeight(double w, double h, double d, double m) does not call a super constructor explicitly. 你的构造函数BoxWeight(double w, double h, double d, double m)不会显式调用超级构造函数。 This means that the default constructor Box() is called. 这意味着调用默认构造函数Box()

If you remove that constructor or make it inaccessible from BoxWeight you will get this error. 如果您删除该构造函数或使其无法从BoxWeight访问,您将收到此错误。

What you can do instead is calling the other constructor explicitly, as you already found out: 您可以做的是显式调用其他构造函数,如您已经发现的那样:

BoxWeight(double w, double h, double d, double m) {

        super(w,h,d);
        this.h = h;
        this.d = d;
        this.w = w;
        weight = m;
}

An inherited class can only be constructed by calling a constructor on the super class. 只能通过调用超类上的构造函数来构造继承的类。

When you are creating objects of a sub class, if you don't explicitly make a call to the constructor of the immediate super class of the class, then implicitly, the default constructor of the immediate super class is called (and this chain continues up the inheritance hierarchy). 当您创建子类的对象时,如果您没有显式调用该类的直接超类的构造函数,则隐式地,调用该直接超类的默认构造函数(并且此链继续运行)继承层次结构)。

This is what you are doing with the call to super(w, h, d) . 这就是你对super(w, h, d)的调用。 You are explicitly making a call to the constructor of the super class of Box. 您正在显式调用Box的超类构造函数。 In this case, the implicit call to super() is omitted. 在这种情况下,省略对super()的隐式调用。 super() is a call to the default constructor of the super class. super()是对超类的默认构造函数的调用。 But since you have not included a default constructor, it results in an error. 但由于您没有包含默认构造函数,因此会导致错误。

However, say you add a default constructor to your Box class. 但是,假设您向Box类添加了一个默认构造函数。 Then also this code should work fine, since now, a call to super() invokes the default constructor of the super class 然后这段代码应该可以正常工作,因为现在,对super()调用会调用超类的默认构造函数

When a class does not define its own constructor, then a default implicit parameterless constructor is automatically provided by the language. 当类没有定义自己的构造函数时,语言会自动提供默认的隐式无参数构造函数。

When a class defines at least one constructor, then no implicit constructor is provided by the language, so the class may only be instantiated using one of the constructors that it defines. 当一个类定义至少一个构造函数时,该语言不提供隐式构造函数,因此该类只能使用它定义的构造函数之一进行实例化。

When the constructor of a derived class does not invoke any constructor of its super class, then the language automatically supplies an implicit call to super() . 当派生类的构造函数不调用其超类的任何构造函数时,该语言会自动提供对super()的隐式调用。 But for this to work, the super class must have an (explicit or implicit) parameterless constructor. 但为了使其工作,超类必须具有(显式或隐式)无参数构造函数。

So, what is happening with the above code is that the constructor of BoxWeight is not explicitly invoking super() , so an implicit call to super() is provided by the language. 所以,上面的代码发生的是BoxWeight的构造函数没有显式调用super() ,因此语言提供了对super()的隐式调用。 This works, because Box defines an explicit parameterless constructor Box() . 这是有效的,因为Box定义了一个显式的无参数构造函数Box()

When you remove the explicit parameterless constructor Box() then the compiler has no constructor of Box to invoke from within the BoxWeight constructor. 当你删除显式无参数构造函数Box() ,编译器没有Box构造函数从BoxWeight构造函数中调用。 It cannot invoke the remaining constructor, because it takes parameters, and the compiler cannot just decide for you what parameters to pass. 它不能调用剩余的构造函数,因为它接受参数,编译器不能只决定要传递的参数。 And as you have discovered by yourself, if you supply a call to the constructor of Box which accepts parameters, your code compiles again. 正如您自己发现的那样,如果您调用接受参数的Box的构造函数,您的代码将再次编译。

Also, please note for future reference: "It gives an error to me" is not an adequate description of a problem. 另外,请注意以备将来参考:“它给我一个错误”并不是对问题的充分描述。 It gives you a very specific error, which you must include in your question. 它会为您提供一个非常具体的错误,您必须在问题中包含该错误。 The error is: "constructor Box in class Box cannot be applied to given types". 错误是:“类Box中的构造函数Box不能应用于给定类型”。

What is actually happening in your program when you remove the default constructor Box then the compiler has no constructor to invoke from within the BoxWeight constructor. 删除默认构造函数Box时,程序中实际发生的情况是编译器没有从BoxWeight构造函数中调用的构造函数。 This creates a confusion for the compiler and hence it gives you an error. 这会给编译器带来混乱,因此会给您一个错误。

But however, when you supply a call to the constructor of Box that accepts parameters, your code compiles without any errors. 但是,当您提供对接受参数的Box的构造函数的调用时,您的代码将编译而没有任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM