简体   繁体   English

类构造函数中的stackoverflow错误

[英]stackoverflow error in class constructor

Please excuse what is probably a very basic question, but I am writing a program to store employee info and it works fine until it tries to set the info inside my employee class.请原谅这可能是一个非常基本的问题,但我正在编写一个程序来存储员工信息,并且它可以正常工作,直到它尝试在我的员工类中设置信息。 It gives a stackoverflow error and I cannot figure out why.它给出了一个 stackoverflow 错误,我不知道为什么。 Thanks for any help.谢谢你的帮助。

Main class:主要类:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("Enter the number of employees to enter.");
        int employeeCount = Input.nextInt();
        Input.nextLine();

        Employee employee[] = new Employee[employeeCount];
        String namesTemp;
        String streetTemp;
        String cityTemp;
        String stateTemp;
        String zipCodeTemp;
        String address;
        String dateOfHireTemp;

        for(int x = 0; x < employeeCount; x++)
        {
            System.out.println("Please enter the name of Employee " + (x + 1));
            namesTemp = Input.nextLine();
            System.out.println("Please enter the street for Employee " + (x + 1));
            streetTemp = Input.nextLine();
            System.out.println("Please enter the city of Employee " + (x + 1));
            cityTemp = Input.nextLine();
            System.out.println("Please enter the state of Employee " + (x + 1));
            stateTemp = Input.nextLine();
            System.out.println("Please enter the zip code of Employee " + (x + 1));
            zipCodeTemp = Input.nextLine();
            address = streetTemp + ", " + cityTemp + ", " + stateTemp + ", " + zipCodeTemp;
            System.out.println("Please enter the date of hire for Employee " + (x + 1));
            dateOfHireTemp = Input.nextLine();
            System.out.println("The employee ID for employee " + (x + 1) + " is " + (x + 1));
            employee[x] = new Employee(x, namesTemp, address, dateOfHireTemp);
        }
    }
}

Employee class:员工类:

public class Employee
{
    private int employeeID;
    private Name name;
    private Address address;
    private DateOfHire hireDate;

    public Employee()
    {

    }

    public Employee(int employeeID, String name, String address, String hireDate)
    {
        String temp;
        Name employeeName = new Name(name);
        this.employeeID = employeeID;
    }
}

Name class:名称类:

public class Name 
{
    public Name name;

    public Name(String name)
    {
        Name employeeName = new Name(name);
        this.name = employeeName;
    }
}

The most common cause of StackoverflowExceptions is to unknowingly have recursion, and is that happening here? StackoverflowExceptions 最常见的原因是在不知不觉中发生了递归,这是否发生在这里? ... ...

public Name(String name)
{
    Name employeeName = new Name(name);  // **** YIKES!! ***
    this.name = employeeName;
}

Bingo: recursion!宾果游戏:递归!

This constructor will create a new Name object whose constructor will create a new Name object whose constructor will... and thus you will keep creating new Name objects ad infinitum or until stack memory runs out.这个构造函数将创建一个新的 Name 对象,它的构造函数将创建一个新的 Name 对象,它的构造函数将......因此你将无限地继续创建新的 Name 对象,或者直到堆栈内存耗尽。 Solution: don't do this.解决方案:不要这样做。 Assign name to a String:为字符串分配名称:

class Name {
    String name; // ***** String field!

    public Name(String name)
    {
        this.name = name;  // this.name is a String field
    }

Typically a class is used to group data together with functionality.通常,类用于将数据与功能组合在一起。 It appears that the Name class is simply a wrapper for a String without adding any functionality.看起来Name类只是一个String的包装器,没有添加任何功能。 At this point in your Java career, it is probably better to declare String name;在你的 Java 职业生涯的这个阶段,声明String name;可能更好String name; in the Employee class and remove the Name class all together.Employee类中并一起删除Name类。 (Note that this would remove the error from your code that Hovercraft Full of Eels described.) (请注意,这会从您的代码中消除充满鳗鱼的气垫船所描述的错误。)

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

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