得到堆栈溢出错误,我不确定为什么

die*_*zin 0 java stack-overflow

编辑:

主要方法......

创造一个新的球员.

球员班......

创建一个手的实例.

手班......

创造一个arraylist

就这样.它非常简单

public class Player 
{
/*------------------------
 * instantiating variable
 -----------------------*/
protected Hand hand;
protected boolean active = false;

/*------------
 * constructor
 -----------*/
    public Player()
{
    hand = new Hand();
    hand.setSize(5);
}



public class Hand extends Player
    {
/*-----------------------------------------
 * variable declaration
 ----------------------------------------*/
ArrayList <Card> hand;
protected int size;
Card temp;

     /*------------------------------------------
 * constructor
 * creates arraylist of cards to keep in hand
 ------------------------------------------*/
public Hand()
{
    hand = new ArrayList<Card>();
}

/*-------------------------------
 * sets the size of the max hand
 ------------------------------*/
public void setSize(int newSize)
{
    size = newSize;

}
Run Code Online (Sandbox Code Playgroud)

编辑:错误是:

线程"main"java.lang.StackOverflowError中的异常

at Player.<init>(Player.java:19)

at Hand.<init>(Hand.java:21)
Run Code Online (Sandbox Code Playgroud)

玩家中的第19行是"公共玩家()"

Hand的第21行是"public Hand()"

仅供参考

NPE*_*NPE 6

Hand扩展Player,因此拥有所有Player的数据成员,包括

protected Hand hand;
Run Code Online (Sandbox Code Playgroud)

要初始化那些继承的成员,Hand构造函数会隐式调用它们Player.

  1. 你打电话给Hand构造函数.
  2. 它调用Player的是构造函数.
  3. Player的构造函数执行new Hand(),循环无限重复,直到你的堆栈空间用完为止.