首页 / JAVA / 在初始化之外的Java中创建对象
在初始化之外的Java中创建对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在初始化之外的Java中创建对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3143字,纯文字阅读大概需要5分钟。
内容图文
![在初始化之外的Java中创建对象](/upload/InfoBanner/zyjiaocheng/657/264d46b430b54ff98fc69b009ee6894d.jpg)
因此对于我正在创建的游戏,我有一些扩展GameDriver的类.
到目前为止,在所有其他类上,我都可以扩展GameDriver,然后在GameDriver中可以执行以下操作:
ArrayList<Card> library = new ArrayList<Card>();
今天,我开始学习GameAI类,并扩展了GameDriver,并且当我提出以下内容时:
GameAI Enemy = new GameAI();
在同一位置,我放置了另一行代码(在公共类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)
如果我将其放在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);
如果您想要一个数组或它们的列表,则可以循环执行.
内容总结
以上是互联网集市为您收集整理的在初始化之外的Java中创建对象全部内容,希望文章能够帮你解决在初始化之外的Java中创建对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。