继承类(Java),显式构造函数错误消息

继承类(Java),显式构造函数错误消息,java,inheritance,compilation,extends,Java,Inheritance,Compilation,Extends,所以我试图学习继承类 首先,我创建了一个名为Box的类来计算长方体的面积 然后我创建了一个TestBox类,在其中我创建了一个名为fedEx的box对象 盒子类: public class Box { private String boxName; public void calculateArea(int length, int width) { System.out.println("Area of " + getBoxInfo() + (length *

所以我试图学习继承类

首先,我创建了一个名为Box的类来计算长方体的面积

然后我创建了一个TestBox类,在其中我创建了一个名为fedEx的box对象

盒子类:

public class Box {
    private String boxName;

    public void calculateArea(int length, int width) {
        System.out.println("Area of " + getBoxInfo() + (length * width));
    }

    public Box(String boxName) {
        this.boxName = boxName;
    }

    public String getBoxInfo() {
        return boxName;
    }
}
public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);
    }
}
public class NewBox extends Box {

    public void calculateVolume(int length, int width, int height) {
        System.out.println("Volume = " + (length * width * height));
    }
}
TestBox类:

public class Box {
    private String boxName;

    public void calculateArea(int length, int width) {
        System.out.println("Area of " + getBoxInfo() + (length * width));
    }

    public Box(String boxName) {
        this.boxName = boxName;
    }

    public String getBoxInfo() {
        return boxName;
    }
}
public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);
    }
}
public class NewBox extends Box {

    public void calculateVolume(int length, int width, int height) {
        System.out.println("Volume = " + (length * width * height));
    }
}
到目前为止,如果我运行这段代码,一切正常,我的打印屏幕显示 联邦快递46的面积

现在我创建了一个名为NewBox的新类,并使用“extends”从类框继承方法,这个类用于计算体积

新箱类:

public class Box {
    private String boxName;

    public void calculateArea(int length, int width) {
        System.out.println("Area of " + getBoxInfo() + (length * width));
    }

    public Box(String boxName) {
        this.boxName = boxName;
    }

    public String getBoxInfo() {
        return boxName;
    }
}
public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);
    }
}
public class NewBox extends Box {

    public void calculateVolume(int length, int width, int height) {
        System.out.println("Volume = " + (length * width * height));
    }
}
现在为了测试这一点,我在TestBox类中创建了一个名为UPS的新对象,现在我的TestBox类如下所示:

public class TestBox {

    public static void main(String[] args) {

        Box fedEx = new Box("fedEx");
        fedEx.calculateArea(23, 2);

        NewBox UPS = new NewBox("UPS");
        UPS.calculateArea(3, 2);
        UPS.calculateVolume(3, 2, 2);
    }
}
当我尝试运行此程序时,会收到以下错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor NewBox(String) is undefined
    at day3.inheritence.TestBox.main(TestBox.java:10)
我使用eclipse作为我的IDE


我能做些什么来修复我的代码,错误消息意味着什么

在您的
NewBox
类中添加以下内容

public NewBox(String name){
    super(name);
}
现在,
NewBox
变成

public class NewBox extends Box{
    public NewBox(String name){
        super(name);
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}

在您的
NewBox
类中添加以下内容

public NewBox(String name){
    super(name);
}
现在,
NewBox
变成

public class NewBox extends Box{
    public NewBox(String name){
        super(name);
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}

NewBox必须有一个转发到父类构造函数的构造函数。试试这个:

public class NewBox extends Box{ 

  public NewBox(String name) {
    super(name);
  }

  public void calculateVolume(int length, int width, int height){ 
    System.out.println("Volume = " + (length*width*height));
  }
}

NewBox必须有一个转发到父类构造函数的构造函数。试试这个:

public class NewBox extends Box{ 

  public NewBox(String name) {
    super(name);
  }

  public void calculateVolume(int length, int width, int height){ 
    System.out.println("Volume = " + (length*width*height));
  }
}
由于父类(Box)有一个参数化构造函数,所以子类(NewBox)必须有一个参数化构造函数,然后该构造函数将调用其超级构造函数

public NewBox(String name){
   super(name)
}
由于父类(Box)有一个参数化构造函数,所以子类(NewBox)必须有一个参数化构造函数,然后该构造函数将调用其超级构造函数

public NewBox(String name){
   super(name)
}
我们先来谈谈。如果您自己不创建构造函数,则默认构造函数将隐式声明

public class SomeClass {
    // field and method declarations to follow
}
如果声明构造函数,则没有默认构造函数;只有声明的构造函数

对于继承,如果子级隐式声明默认构造函数,但父级没有具有类似签名的构造函数,则会发生编译时错误

更具体地说:

如果隐式声明了默认构造函数,但超类没有可访问的构造函数(§6.6),且该构造函数不带参数且没有throws子句,则这是编译时错误。

在您的例子中,添加
super
关键字,并将参数直接传递给父级

public class NewBox extends Box {
    public NewBox(String boxName) {
        super(boxName);
    }
}

作为另一个示例,类
Beta
将很好地编译,因为
Alpha
声明了一个与默认构造函数的签名匹配的构造函数,但是
Delta
将不会编译,因为
Gamma
没有匹配的构造函数

class Alpha {
    public Alpha() {

    }
}

class Beta extends Alpha {

}

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }
}

class Delta extends Gamma {

}
这可以通过为'Gam'提供无参数构造函数来修复

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }

    public Gamma() {
        count = 0;
    }
}
我们先来谈谈。如果您自己不创建构造函数,则默认构造函数将隐式声明

public class SomeClass {
    // field and method declarations to follow
}
如果声明构造函数,则没有默认构造函数;只有声明的构造函数

对于继承,如果子级隐式声明默认构造函数,但父级没有具有类似签名的构造函数,则会发生编译时错误

更具体地说:

如果隐式声明了默认构造函数,但超类没有可访问的构造函数(§6.6),且该构造函数不带参数且没有throws子句,则这是编译时错误。

在您的例子中,添加
super
关键字,并将参数直接传递给父级

public class NewBox extends Box {
    public NewBox(String boxName) {
        super(boxName);
    }
}

作为另一个示例,类
Beta
将很好地编译,因为
Alpha
声明了一个与默认构造函数的签名匹配的构造函数,但是
Delta
将不会编译,因为
Gamma
没有匹配的构造函数

class Alpha {
    public Alpha() {

    }
}

class Beta extends Alpha {

}

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }
}

class Delta extends Gamma {

}
这可以通过为'Gam'提供无参数构造函数来修复

class Gamma {
    private final int count;

    public Gamma(int count) {
        this.count = count;
    }

    public Gamma() {
        count = 0;
    }
}

你的类NewBox应该是这样的

public class NewBox extends Box{

    public NewBox(String boxName){
       super(boxName);        
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}

通过写入
NewBox UPS=new NewBox(“UPS”)
在您的测试框中,JVM希望在NewBox中使用字符串作为参数的构造函数,但在那里缺少它。因此,我们在NewBox中创建了一个以字符串作为参数的构造函数,在该构造函数中,我们编写了
super(boxName)
,它说调用父类的构造函数,在这里是Box。

您的类NewBox应该是这样的

public class NewBox extends Box{

    public NewBox(String boxName){
       super(boxName);        
    }

    public void calculateVolume(int length, int width, int height){
        System.out.println("Volume = " + (length*width*height));
    }
}

通过写入
NewBox UPS=new NewBox(“UPS”)
在您的测试框中,JVM希望在NewBox中使用字符串作为参数的构造函数,但在那里缺少它。因此,我们在NewBox中创建了一个以字符串作为参数的构造函数,在该构造函数中,我们编写了
super(boxName)
,它说调用父类的构造函数,这里是Box。

这样做是可行的,但我不明白为什么,您介意解释一下为什么这样做吗?在Java中,父类构造函数不是由子类继承的。因此,您必须在子类上创建一个构造函数,然后告诉该构造函数调用父类上的构造函数。“super”关键字引用父类。有关super关键字的更多信息,请参阅。这样做是可行的,但我不明白为什么,您介意解释一下为什么这样做吗?在Java中,父类构造函数不是由子类继承的。因此,您必须在子类上创建一个构造函数,然后告诉该构造函数调用父类上的构造函数。“super”关键字引用父类。有关super关键字的更多信息,请参阅。