子类的构造函数中的堆栈溢出错误 [英] Stack Overflow Error in the constructor of a subclass

查看:48
本文介绍了子类的构造函数中的堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的超类是:

public abstract class MarketProduct {
private String name;

public MarketProduct(String productName) {
name = productName;
}

public final String getName() {
return this.name;
}

public abstract int getCost();

public abstract boolean equals(Object o);
}

我的子类(直到其构造函数)是:

And my subclass (up until its constructor) is:

public class Egg extends MarketProduct {
 private int numEggs;
 private int priceEggs;

 public Egg(String productName, int numRequired, int priceEggsDozen) {
 super(productName); 
 numEggs = numRequired;
 priceEggs = priceEggsDozen;
 MarketProduct marketProductEgg = new Egg(productName, numEggs, priceEggs);
 }

我在Egg.init(Egg.java:9)处收到java.lang.StackOverflowError.在这种情况下,第9行是子类中构造函数的最后一行,即:

I am getting a java.lang.StackOverflowError at Egg.init(Egg.java:9). Line 9 in this case is the last line of the constructor in the subclass, i.e:

MarketProduct marketProductEgg = new Egg(productName, numEggs, priceEggs);

我了解到,递归方法不断被调用时,会出现堆栈oveflow错误.因此,我认为超类的getName方法中的"this"可能存在问题.但是删除它仍然会在运行时导致错误.我觉得它们是我的子类构造函数中代码结构的问题,但我不确定到底是什么.我尝试使用原始变量在更高的位置创建对象,但无济于事.

I understand that a stack oveflow error arises when a recursive method keeps getting called. So I assumed that may have been a problem with the "this" in the getName method of the superclass. But removing it still caused the error at runtime. I feel that their is a problem in the structure of the code in my subclass constructor, but I am not sure what exactly. I tried to create the object higher up with the original variables, but to no avail.

有帮助吗?

推荐答案

编写时,您将在Egg构造函数中创建一个新Egg

You're creating a new Egg in the Egg constructor when you write

MarketProduct marketProductEgg = new Egg(productName, numEggs, priceEggs);

这意味着每次创建一个Egg时,构造函数都会运行,然后在构造函数中创建一个新的Egg,这会使构造函数再次运行并创建一个新的Egg,然后构造函数再次运行,从而导致一个新的要创建的鸡蛋,然后构造函数再次运行...

That means every time you make an Egg, the constructor will run, then in the constructor you create a new egg, which causes the constructor to run again and create a new egg, then the constructor runs again, which causes a new egg to be created, then the constructor runs again...

您在这里看到了重点.每次创建一个鸡蛋时,都会创建另一个鸡蛋,直到您有无限个鸡蛋(或者堆栈空间用完,导致 StackOverflowError ).

You see the point here. Every time an egg is created, another egg will be created, until you have infinite eggs (or you run out of stack space, causing a StackOverflowError).

由于您从不使用 marketProductEgg ,因此只需从构造函数中删除它即可.

Since you never use marketProductEgg, just get rid of it from the constructor.

为了证明这与子类化无关,请采用此类:

And just to prove that this has nothing to do with subclassing, take this class:

class A {
   A() {
     A(); // Creates a new A in the constructor of A
   } 
}

并尝试创建它的实例.每个A都会创建另一个A,另一个A会创建另一个A ...

And try to create an instance of it. Every A creates another A, which creates another A, which creates another A...

这篇关于子类的构造函数中的堆栈溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆