简体   繁体   English

Java 中的单个 Inheritance 问题

[英]Single Inheritance issue in Java

I tried to execute the below java code which demonstrates single inheritance.我尝试执行以下 java 代码,该代码演示了单个 inheritance。

class Box {
    double width;
    double height;
    double depth;
    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }
    double volume() {
        return width*height*depth;
    }
}

class BoxWeight extends Box {
    double mass;
    BoxWeight(double w, double h, double d, double m) {
        width = w;
        height = h;
        depth = d;
        mass = m;
    }
}

class BoxDemo {
    public static void main(String[] args) {
        BoxWeight box = new BoxWeight(10,20,15,32.3);
        double vol;
        vol = box.volume();
        System.out.println("Volume = " + vol);
    }
}

But this raised the following error:但这引发了以下错误:

C:\User\Java>javac BoxWeight.java
BoxWeight.java:16: error: constructor Box in class Box cannot be applied to given types;
    BoxWeight(double w, double h, double d, double m) {
                                                      ^
  required: double,double,double
  found:    no arguments
  reason: actual and formal argument lists differ in length
1 error

Again when I modified the code in sub class constructor as below当我再次修改子 class 构造函数中的代码时,如下所示

BoxWeight(double w, double h, double d, double m) {
        super(w,h,d);
        mass = m;
    }

it gave the desired output.它给出了所需的 output。

Why the first way didn't work?为什么第一种方法不起作用?

You have to pass the width, height and depth to the super class constructor:您必须将宽度、高度和深度传递给超级 class 构造函数:

BoxWeight(double w, double h, double d, double m) {
    super(w,h,d);
    mass = m;
}

When the sub-class constructor doesn't contain the super(...) call to the super-class constructor, the compiler implicitly adds a super() call to the super-class parameter-less constructor, which doesn't exist in your Box class.当子类构造函数不包含对超类构造函数的super(...)调用时,编译器会隐式添加对超类无参数构造函数的super()调用,这在你的Box class。 That's the reason your code didn't pass compilation.这就是您的代码没有通过编译的原因。

As Amongalen commented, if you don't define any constructor in your class, the compiler adds a default parameter-less constructor.正如中间人评论的那样,如果您没有在 class 中定义任何构造函数,编译器会添加一个默认的无参数构造函数。 However, once you define any constructor, the default constructor is not added.但是,一旦定义了任何构造函数,就不会添加默认构造函数。 This means that your code could have passed compilation if you just removed the Box(double w, double h, double d) constructor (or kept that constructor and added an explicit Box() constructor).这意味着如果您刚刚删除Box(double w, double h, double d)构造函数(或保留该构造函数并添加显式Box()构造函数),您的代码可能已经通过编译。

Always remember a simple rule when working with inheritance使用 inheritance 时始终记住一个简单的规则

Whenever any constructor of a child class is called the default constructor of the parent class is called automatically

Now in our case when we invoked BoxWeight(double w, double h, double d, double m) constructor the default constructor of parent class ie Box() needs to be called automatically.现在,当我们调用BoxWeight(double w, double h, double d, double m)构造函数时,父 class 的默认构造函数即Box()需要自动调用。 But in our case Box() doesn't exist so we need to manually call super(w,h,d);但是在我们的例子中Box()不存在,所以我们需要手动调用super(w,h,d); . . Here super is used to refer to the parent class.这里的 super 用来指父 class。

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

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