与构造函数有一些问题?

New*_*ner 0 java stack-overflow inheritance exception extend

我在构建课程时遇到问题,

以下代码抛出异常

public class Agent extends Board{    
    private static boolean arrow;
    private static boolean backmove;
    private static boolean gotGold;

    private static int atlocationX;
    private static int atlocationY;
    private static int faceposition; 
    private static int pathcounter;

    public boolean temporaryGold;
    public boolean temporaryWumpus;
    public boolean temporaryStench;
    public boolean temporaryBreeze;
    public boolean temporaryPit;

    int agentgui = 0;
    int label = 0;

    Block blockstorage[][] = new Block[4][4];
    KnowledgeBase kb = new KnowledgeBase();

    public Agent() {
        super();
    }

    public void Agent(){
        Agent.arrow = true;
        Agent.faceposition = 2;
        Agent.atlocationX = 0;
        Agent.atlocationY = 0;
        Agent.backmove = false;
        Agent.gotGold = false;
        Agent.pathcounter = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在于,当我尝试添加时

Agent agent = new Agent();
Run Code Online (Sandbox Code Playgroud)

在我的一些其他类中,例如:

public class Board {
    Agent agent = new Agent();
}
Run Code Online (Sandbox Code Playgroud)

它返回一个错误:

线程"AWT-EventQueue-0"中的异常java.lang.StackOverflowError

我也在我的主要尝试过,但仍然是相同的.

是什么导致这种例外?

ami*_*mit 5

您正在使用间接递归调用进行无限循环.

构造Board对象需要一个Agent对象,反之亦然