java - 收到 StackOverFlowError 且不确定原因?

标签 java class methods stack-overflow extends

我试图创建的程序是一个基本游戏,用户输入网格大小,选择 block 接收增加分数的奖品、从分数中夺走分数的强盗或结束游戏的炸弹。我收到堆栈流错误,但我不明白为什么?

抱歉,代码量很大,我只是无法找到问题所在!

这是我收到的堆栈溢出错误。它发生在你输入​​网格大小之后(它比这个长得多,因为你可以看到游戏项目、 block 跳和奖品不断重复:

java.lang.StackOverflowError
at java.lang.System.nanoTime(Native Method)
at java.util.Random.<init>(Random.java:62)
at BlockHop.<init>(BlockHop.java:12)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)

block 跳 GUI:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BlockHopGUI extends JFrame{

  private BlockHop bh;
  private JButton [][] board;
  private JLabel scorePoints;
  private PlayHandler ph;  // listener for buttons

  public BlockHopGUI( int gridSize ) {
    super( "Click to uncover prizes" );
    bh = new BlockHop( gridSize );
    System.out.println( "gridsize: " + gridSize );
    Container c = getContentPane( );
    JPanel p = new JPanel( );
    board = new JButton[gridSize][gridSize];
    p.setLayout( new GridLayout( gridSize, gridSize ) );

    ph = new PlayHandler( );
    for ( int row = 0; row < board.length; row++ )
      for ( int col = 0; col < board.length; col++ ) {
      board[row][col] = new JButton( "" );
      board[row][col].addActionListener(ph);
      p.add( board[row][col] );
    }
    c.add( p, BorderLayout.CENTER );

    JPanel scorePanel = new JPanel( );
    JLabel scoreLabel = new JLabel( "Score: " );
    scorePoints = new JLabel( Integer.toString( bh.getScore( ) ) );
    scorePanel.add( scoreLabel );
    scorePanel.add( scorePoints );

    c.add( scorePanel, BorderLayout.SOUTH );

    setSize( 500, 500 );
    setVisible( true );  
  }

  private class PlayHandler implements ActionListener {
    public void actionPerformed( ActionEvent ae ) {
      for ( int row = 0; row < board.length; row++ ) 
        for ( int col = 0; col < board[0].length; col++ ) {
        if ( ae.getSource( ) == board[row][col] ) {
         bh.play( row, col );
         board[row][col].setText( bh.getLabel( row, col ) );
         board[row][col].removeActionListener( ph );
         break;
        }
      }
      scorePoints.setText(  Integer.toString( bh.getScore( ) ) );
      if ( bh.isGameOver( ) ) {
        JOptionPane.showMessageDialog( null, "Game over! Final points: " 
                                    + bh.getScore( ) );
        System.exit( 1 );   
      }

block 跳跃等级:

import java.util.*;
import java.awt.*;
import javax.swing.*;

public class BlockHop {

  Random rand = new Random();
  private GameItems [] [] board;
  int score = 100;
  int row;
  int col;

  public BlockHop () {
    board = new GameItems [1][1];
    board[0][0] =  new Prize( 0, 0, 'P') ;
  }

  public BlockHop( int gridSize )  {
    board = new GameItems [gridSize][gridSize];
    int end = 10;
    int start = 1;
       for ( int row = 0; row < board.length; row++ )
        for ( int col = 0; col < board[row].length; col++ ) {

    int numAssign = rand.nextInt( end - start + 1 ) + start;
       // System.out.print("test");
       switch (numAssign) {
      case 1: case 2 : case 3 : case 4 : case 5 : case 6 :
        board[row][col] =  new Prize( row, col, 'P') ;
        System.out.print("test");
        break;
      case 7:
        board[row][col] = ( new Bomb( row, col, 'B') );
        System.out.print("test");
        break;
    case 8: case 9 : case 10 :
        board[row][col] = ( new Bandit( row, col, 'X') );
        System.out.print("test");
        break;
    }
   }

  }

  public void  play( int row, int col )  {
  String newID = getLabel(row, col);
   //newID.adjustScore()

  }


   public int getScore( ) {

    return score;
  }



  public String getLabel(int row, int col) {
   if ((board[row][col]).equals('P'))
     return "p";
   else if ((board[row][col]).equals('X'))
     return "x";
   else
     return "b";

  }

  public boolean isGameOver( ) {
    if ( getScore() < 0)
      return true;
    else
   return false; 
  }



}

游戏元素类别:

import java.awt.*;
import javax.swing.*;
import java.io.*;

public abstract class GameItems extends BlockHop {

  private char ID;  // block type
  private int row;      // row 
  private int col;      // column


  public GameItems (){
    ID = ' ';
    row = 0;
    col = 0;
  }

  public GameItems (int newRow, int newCol, char newId) {
    row = newRow;
    col = newCol;
    ID = newId;


  }

  public int adjustScore(char input) {
    switch (input) {
      case 'P' :
     score += 10;
     break;
      case 'X':
     score -= 5;
     break;
      case 'B' :
     score -= 2000;
     break;

  }
    return score;

    }
    }

奖项类别:

public class Prize extends GameItems{
  public Prize () {
    super();
  }

  public Prize (int row, int col, char id) {
    super (row, col, id);
  }
}

炸弹类别:

public class Bomb extends GameItems {
  public Bomb () {
    super();
  }

  public Bomb (int row, int col, char id) {
    super (row, col, id);
  }
}

强盗等级:

public class Bandit extends GameItems {
  public Bandit () {
    super();
  }

  public Bandit (int row, int col, char id) {
    super (row, col, id);
  }
}

最佳答案

这确实是构造函数中的堆栈溢出..

BlockHop你有:

  public BlockHop () {

    board = new GameItems [1][1]; 
    prize = new Prize(...);
  }

但是你有GameItems定义为:

public abstract class GameItems extends BlockHop {
//...
}

每次构造一个继承自 GameItems 的类的实例时,它构造了一个 BlockHop它构造了一个 Prize它继承GameItems .. 为什么?因为:

public class Prize extends GameItems {
//...
}

所以现在每次你构建一个奖品时,它都会构建一个区 block 跳跃,反之亦然。

关于java - 收到 StackOverFlowError 且不确定原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897343/

相关文章:

java - 数组列表插入方法

c++ - "overloaded member function not found"引用

Java GET 返回日期为 13 位数字

javascript - 这不是 javascript 类构造函数中定义的错误?

java - 您将如何使用不同的对象在 Python 中编写自定义类?

java - 类是否应该负责检查是否已创建可行的对象?如果是这样怎么办?

javascript - 不同条件下的 React 调用方法

java - 什么使这个上下文静态?怎么办?

java - 在初始化私有(private)静态变量时使用 getClass() 进行同步是不是一个坏主意?

java - 使用 servlet 发送电子邮件