gpt4 book ai didi

java - 在 init 之外的 java 中创建对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:19 25 4
gpt4 key购买 nike

所以对于我正在创建的游戏,我有几个扩展 GameDriver 的类。

到目前为止,我已经能够在所有其他类上扩展 GameDriver,然后在 GameDriver 中我可以:

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

今天我开始学习 GameAI 类并扩展了 GameDriver,当我输入时:

GameAI Enemy = new GameAI();

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

我得到:

java.lang.StackOverflowError
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)

如果我把它放在我的小程序的 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,因为这是共享信息的错误方式,即使您没有这种递归噩梦,它也根本无法工作。而是给 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);

如果你想要一个数组或它们的列表,你可以在循环中执行此操作。

关于java - 在 init 之外的 java 中创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com