在init之外的java中创建一个对象 [英] Creating an Object in java outside of the init

查看:127
本文介绍了在init之外的java中创建一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



直到这一点,在所有其他类以扩展GameDriver,然后在GameDriver中我可以做:

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

今天我开始了GameAI类,我扩展了GameDriver,

  GameAI Enemy = new GameAI(); 



在同一个地方,我把另一行代码放在public class GameDriver下面



我得到:

  java.lang.StackOverflowError 
at java .util.WeakHashMap.expungeStaleEntries(未知源)
at java.util.WeakHashMap.getTable(未知源)
at java.util.WeakHashMap.get(未知来源)
at java.awt .Component.checkCoalescing(未知源)
at java.awt.Component。< init>(未知源)
at java.awt.Container< init>(未知源)
< init>(未知源)
at java.awt.Panel。< init>(未知源)
at java.applet.Applet。未知来源)
在GameDriver。< init>(GameDriver.java:14)
在GameAI。< init>(GameAI.java:8)
在GameDriver。 (GameDriver.java:40)
在GameAI。< init>(GameAI.java:8)


$ b b

如果我把它放在我的小程序的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实现MouseMotionListener,MouseListener {

Graphics g;
图片offscreen;
尺寸昏暗;

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);
}

...下面有更多的方法和东西,但与GameAI敌人无关


解决方案

您正在GameDriver中创建一个GameAI对象,它扩展的类。这将导致递归继续,直到你用完内存。



解决方案:不要这样做。你的GameAI类不应该扩展GameDriver,因为这是错误的方式来共享信息,它只是不会工作,即使你没有这个递归的噩梦。而是给GameAI一个GameDriver字段,并通过其构造函数将GameDriver实例传递给GameAI。



  class GameAI {
private GameDriver gameDriver;

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

// ....更多代码
}

编辑2

如果您想要一个GameAI对象,请执行

  GameAI gameAi = new GameAI(this); 

如果你想要一个数组或列表,你可以循环。 p>

so for the game I'm creating I have a few classes that extend GameDriver.

Up until this point, on all the other classes I've been able to extend GameDriver and then In GameDriver I can do:

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

Today I started on the GameAI class and I extended GameDriver, and when I put:

GameAI Enemy = new GameAI();

In the same spot I put the other line of code (Right below public class GameDriver)

I get:

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)

If I put it in the public void init() of my applet, then it doesn't give an error on run, but then I wouldn't be able to access it from my other methods, am I over looking something? All nighters normally don't help my brain...

This is what the GameAI looks like as of right now:

public class GameAI extends GameDriver {

    public int life;
    public int energy;

    public void drawPhase(){

    }

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

And then some bits of the 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

解决方案

You're creating a GameAI object inside of GameDriver, the class it extends. This will cause recursion to continue until you run out of memory.

Solution: don't do this. Your GameAI class should not extend GameDriver as that's the wrong way to share information and it simply won't work even if you didn't have this recursion nightmare. Instead give GameAI a GameDriver field and pass the GameDriver instance into GameAI through its constructor.

i.e.,

class GameAI {
   private GameDriver gameDriver;

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

   //.... more code
}  

Edit 2
If you want one GameAI object, you'd do

GameAI gameAi = new GameAI(this);

If you want an array or List of them, you'd do this in a loop.

这篇关于在init之外的java中创建一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆