Java ArrayList StackOverFlowError
Java ArrayList StackOverFlowError
第一次提问,请耐心等待。我正在做一项学校作业,并且一直在与一个我不太了解的 Whosebug 错误作斗争,无法深入了解。错误由 ~~~:
表示
堆栈跟踪中的代码部分如下:
public class Employee extends StaffMember {
public static final int DEFAULT_SIN = 123456789;
public static final double MINIMUM_WAGE = 400.00;
protected int socialInsuranceNumber = DEFAULT_SIN;;
protected double payRate = MINIMUM_WAGE;
/**
*
*/
public Employee() {
super();
}
/**
* @param name
* @param streetAddress
* @param phone
* @param socialInsuranceNumber
* @param payRate
*/
public Employee(String name, String address, String phone, int socialInsuranceNumber, double payRate) {
~~~ super(name, address, phone);
this.socialInsuranceNumber = socialInsuranceNumber;
this.payRate = payRate;
}
public abstract class StaffMember extends Staff {
public static final String DEFAULT_NAME = "Default Name";
public static final String DEFAULT_ADDRESS = "Default Address";
public static final String DEFAULT_PHONE = "Default Phone";
protected String name;
protected String address;
protected String phone;
/**
* default constructor
*/
public StaffMember() {
super();
}
/**
*
* @param name
* @param address
* @param phone
* abstract class can not be instantiated therefore should not
* have constructors
*/
~~~ public StaffMember(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public class Staff {
public ArrayList<StaffMember> staffList;
/**
* Constructor for objects of type Staff.
*/
public Staff() {
staffList = new ArrayList<StaffMember>(6);
~~~ staffList.add(new Executive("Hilary", "203 Whitewater Line", "871-0469", 123456789, 5000, 0));
staffList.add(new Employee("Thomas", "1000 Robson Street", "604-0000", 010203040, 1500));
staffList.add(new Hourly("Condoleeza", "678 Fifth Ave.", "905-0690", 958473625, 18.50, 0));
staffList.add(new Volunteer("Kimberly", "1200 West Point Grey Road", "514-8374"));
staffList.add(new Volunteer("Jean", "321 Shawinigate Lane", "613-7282"));
}
public class Executive extends Employee {
private double bonus;
/**
* Default Constructor
*/
public Executive() {
super();
}
/**
* @param name
* @param address
* @param phone
* @param socialInsuranceNumber
* @param payRate
*/
public Executive(String name, String address, String phone, int socialInsuranceNumber, double payRate, double bonus) {
super(name, address, phone, socialInsuranceNumber, payRate);
this.awardBonus(bonus);
}
您正在对对象进行循环实例化,这导致您的堆栈溢出。
在您的 Staff
构造函数中,您正在实例化许多 class,例如 Executive
和 Employee
。你的 Executive
class extends
Employee
class 和你的 Employee
class extends
你的 StaffMember
class。
当你这样做时,Staff
的构造函数将被再次隐式调用(通过从 Executive
调用 super()
,然后 Employee
然后 StaffMember
) 并重复整个过程,从而消耗所有内存。
摆脱你的 class 那个 extends Staff
可能会解决你的问题(可能有问题的是来自 StaffMember
的 extends
这是调用链中的最后一个)。
存在构造函数调用的循环链。
您的基础 class 构造函数 Staff()
正在实例化子 classes,后者又调用自身构造函数导致无限链,导致 Whosebug
错误。
此外,StaffMember
扩展 Staff
似乎不正确。由于员工是由所有员工组成的全体员工,StaffMember
代表单个员工。两者没有is A
关系
您应该从 StaffMember 中删除 extends Staff
,然后代码应该也能正常工作。
你的classA的构造函数调用classB的构造函数。classB的构造函数调用classA的构造函数。你有一个无限递归调用,这就是你最终得到 WhosebugError
的原因。您将进入构造函数的循环调用。
Java支持class之间的循环依赖,这里的问题只与构造函数相互调用有关。
第一次提问,请耐心等待。我正在做一项学校作业,并且一直在与一个我不太了解的 Whosebug 错误作斗争,无法深入了解。错误由 ~~~:
表示堆栈跟踪中的代码部分如下:
public class Employee extends StaffMember {
public static final int DEFAULT_SIN = 123456789;
public static final double MINIMUM_WAGE = 400.00;
protected int socialInsuranceNumber = DEFAULT_SIN;;
protected double payRate = MINIMUM_WAGE;
/**
*
*/
public Employee() {
super();
}
/**
* @param name
* @param streetAddress
* @param phone
* @param socialInsuranceNumber
* @param payRate
*/
public Employee(String name, String address, String phone, int socialInsuranceNumber, double payRate) {
~~~ super(name, address, phone);
this.socialInsuranceNumber = socialInsuranceNumber;
this.payRate = payRate;
}
public abstract class StaffMember extends Staff {
public static final String DEFAULT_NAME = "Default Name";
public static final String DEFAULT_ADDRESS = "Default Address";
public static final String DEFAULT_PHONE = "Default Phone";
protected String name;
protected String address;
protected String phone;
/**
* default constructor
*/
public StaffMember() {
super();
}
/**
*
* @param name
* @param address
* @param phone
* abstract class can not be instantiated therefore should not
* have constructors
*/
~~~ public StaffMember(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public class Staff {
public ArrayList<StaffMember> staffList;
/**
* Constructor for objects of type Staff.
*/
public Staff() {
staffList = new ArrayList<StaffMember>(6);
~~~ staffList.add(new Executive("Hilary", "203 Whitewater Line", "871-0469", 123456789, 5000, 0));
staffList.add(new Employee("Thomas", "1000 Robson Street", "604-0000", 010203040, 1500));
staffList.add(new Hourly("Condoleeza", "678 Fifth Ave.", "905-0690", 958473625, 18.50, 0));
staffList.add(new Volunteer("Kimberly", "1200 West Point Grey Road", "514-8374"));
staffList.add(new Volunteer("Jean", "321 Shawinigate Lane", "613-7282"));
}
public class Executive extends Employee {
private double bonus;
/**
* Default Constructor
*/
public Executive() {
super();
}
/**
* @param name
* @param address
* @param phone
* @param socialInsuranceNumber
* @param payRate
*/
public Executive(String name, String address, String phone, int socialInsuranceNumber, double payRate, double bonus) {
super(name, address, phone, socialInsuranceNumber, payRate);
this.awardBonus(bonus);
}
您正在对对象进行循环实例化,这导致您的堆栈溢出。
在您的 Staff
构造函数中,您正在实例化许多 class,例如 Executive
和 Employee
。你的 Executive
class extends
Employee
class 和你的 Employee
class extends
你的 StaffMember
class。
当你这样做时,Staff
的构造函数将被再次隐式调用(通过从 Executive
调用 super()
,然后 Employee
然后 StaffMember
) 并重复整个过程,从而消耗所有内存。
摆脱你的 class 那个 extends Staff
可能会解决你的问题(可能有问题的是来自 StaffMember
的 extends
这是调用链中的最后一个)。
存在构造函数调用的循环链。
您的基础 class 构造函数 Staff()
正在实例化子 classes,后者又调用自身构造函数导致无限链,导致 Whosebug
错误。
此外,StaffMember
扩展 Staff
似乎不正确。由于员工是由所有员工组成的全体员工,StaffMember
代表单个员工。两者没有is A
关系
您应该从 StaffMember 中删除 extends Staff
,然后代码应该也能正常工作。
你的classA的构造函数调用classB的构造函数。classB的构造函数调用classA的构造函数。你有一个无限递归调用,这就是你最终得到 WhosebugError
的原因。您将进入构造函数的循环调用。
Java支持class之间的循环依赖,这里的问题只与构造函数相互调用有关。