在初始化之外的Java中创建对象

因此对于我正在创建的游戏,我有一些扩展GameDriver的类.

到目前为止,在所有其他类上,我都可以扩展GameDriver,然后在GameDriver中可以执行以下操作:

ArrayList<Card>  library = new ArrayList<Card>();

今天,我开始学习GameAI类,并扩展了GameDriver,并且当我提出以下内容时:

GameAI Enemy = new GameAI();

在同一位置,我放置了另一行代码(在公共类GameDriver的正下方)

我得到:

java.lang.*Error
at java.util.WeakHashMap.expungeStaleEntries(Unknown Source)
at java.util.WeakHashMap.getTable(Unknown Source)
at java.util.WeakHashMap.get(Unknown Source)
at java.awt.Component.checkCoalescing(Unknown Source)
at java.awt.Component.<init>(Unknown Source)
at java.awt.Container.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.applet.Applet.<init>(Unknown Source)
at GameDriver.<init>(GameDriver.java:14)
at GameAI.<init>(GameAI.java:8)
at GameDriver.<init>(GameDriver.java:40)
at GameAI.<init>(GameAI.java:8)

如果我将其放在applet的public void init()中,则它不会在运行时产生错误,但是我将无法从其他方法访问它,我是否正在寻找某些东西?通常所有的夜宵都不能帮助我的大脑…

截至目前,GameAI的外观如下:

public class GameAI extends GameDriver {

    public int life;
    public int energy;

    public void drawPhase(){

    }

    public GameAI(){
        life = 20;
        energy = 2;
    }
}

然后是GameDriver的一些内容:

public class GameDriver extends Applet implements MouseMotionListener,MouseListener {

Graphics g; 
Image offscreen;
Dimension dim; 

int playerLife = 20;
int playerEnergy = 8;

int xMouse; 
int yMouse;
int lineThickness = 4;
int handSize = 6;
int currentHover;
boolean slotHover;
int currentSelected;
boolean slotClicked;
int currentHoverBoard;
boolean slotHoverBoard;
boolean slotClickedBoard;
int currentSelectedBoard;

boolean canPlace;
ArrayList<Card>  library = new ArrayList<Card>();
ArrayList<Card>  hand = new ArrayList<Card>();
ArrayList<Card>  playerBoard = new ArrayList<Card>();
GameAI Enemy;
int[] handBoxX = new int[handSize];
int[] handBoxY = new int[handSize];
int[] handBoxW = new int[handSize];
int[] handBoxH = new int[handSize];

int[] playerBoardX = new int[8];
int[] playerBoardY = new int[8];
int[] playerBoardW = new int[8];
int[] playerBoardH = new int[8];



public void init(){
    this.setSize(640, 480);
    dim = this.getSize();
    addMouseMotionListener(this);
    addMouseListener(this);
    createLibrary();
    drawFirstHand();
    printHand();


    GameAI Enemy = new GameAI();
    checkEnemy();



    offscreen = createImage(this.getSize().width,this.getSize().height);
    g = offscreen.getGraphics(); 
}

public void checkEnemy(){
    System.out.println(Enemy.energy);
}

... Alot more methods and stuff below, but nothing to do with the GameAI enemy

解决方法:

您正在GameDriver(它扩展的类)内部创建GameAI对象.这将导致递归继续,直到您用尽内存.

解决方案:不要这样做.您的GameAI类不应扩展GameDriver,因为这是共享信息的错误方法,即使您没有此递归梦night,它也根本无法工作.而是给GameAI一个GameDriver字段,并通过其构造函数将GameDriver实例传递给GameAI.

class GameAI {
   private GameDriver gameDriver;

   public GameAI(GameDriver gameDriver) {
      this.gameDriver = gameDriver;
   }

   //.... more code
}  

编辑2
如果您想要一个GameAI对象,则可以

GameAI gameAi = new GameAI(this);

如果您想要一个数组或它们的列表,则可以循环执行.

上一篇:php-类变量的作用域解析运算符


下一篇:有什么理由或用例在php中使用self关键字而不是static关键字?