gpt4 book ai didi

java - 仅当方法被重写时,通过扩展类调用方法才有效

转载 作者:行者123 更新时间:2023-12-02 06:21:50 24 4
gpt4 key购买 nike

我有 4 个类(Core、GameMain、Spriter、Character)
- GameMain 扩展核心(核心 >> GameMain)
- 角色扩展 Spriter (Spriter >> 角色)

如果我调用 - b.get...() - 方法而不在Character中覆盖它们(它们最初位于Spriter内部,我通过Character从GameMain调用它们),我会得到一个NullPointerException

我在Core中创建Character对象并将其放入ArrayList中

public abstract class Core {
ArrayList<Character> mon = new ArrayList<Character>();
void gameloop(){
while(running){
//some code
while(mon.size() < 2){
mon.add(new Character(blk_1,5,1));// Character(Spriter, Long, Long){}
}
draw(g);
}
}
}

然后在 GameMain 中我调用了一个原本在 Spriter 中的方法,但我通过 Character 调用它。

public class GameMain extends Core{
public void draw(Graphics2D g){
for(Character b : mon){ //mon is the ArrayList<Character>

if(test.getCoordY() <= b.getCoordY() - (b.getHeight()/2)){ //<<<< NullPointerException caused by these two Methods , test is a Spriter class

g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), test.getWidth(), (test.getHeight()), null);
g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);

}else {
g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);
g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), null);
}
}
}
}

这是 Spriter 的方法,除非我覆盖它,否则我会收到错误。

public class Spriter {
Spriter(Image i){
this.i = i;
scenes = new ArrayList();

}

Spriter(){
scenes = new ArrayList();
start();
}

public int getHeight(){
return i.getHeight(null);
}
public float getCoordY(){
float cy;
cy = y + i.getHeight(null); //<<<< NullPointerException happens here, Image i;
return cy;
}

public void setX(float x){
this.x = x;
}

public void setY(float y){
this.y = y;
}
// other similar Methods but no problem with them
//-------------------------------------- Animation Part ----------------------------------------------

public synchronized void addScene(Image i,long t){
total_t+=t;
scenes.add(new oneScene(i, total_t));
}

//start animation
public synchronized void start(){
mov_time = 0;
scn_indx = 0;
}

// get current scene
public synchronized Image getAnimeImage(){
if(scenes.size()==0){
return null;
}else{
return getScene(scn_indx).pic;
}
}


//get the scene
private oneScene getScene(int x){
return (oneScene)scenes.get(x);
}


private class oneScene{
Image pic;
long endTime;

public oneScene(Image pic, long endTime){
this.pic = pic;
this.endTime = endTime;

}
}
}

如果我这样做就会起作用:

public class Character extends Spriter{
public Character(Spriter s, long health, long power) {
this.s = s;
this.health = health;
this.power = power;

s.setX(randomY());
s.setY(randomY());
}

public float getCoordY(){
return s.getCoordY();
}

public float getHeight(){
return s.getgetHeight();
}
//some other methods for health and power
}

但是如果我这样做它可以工作吗(它已经给出了错误,但如何避免它):

public class Character extends Spriter{
public Character(Spriter s, long health, long power) {
this.s = s;
this.health = health;
this.power = power;

s.setX(randomY()); //setting x for the dynamiclly generated Character Object
s.setY(randomY()); // same for y
}

//some methods for power and health
}

设置 x,y 进行测试(它是一个 Sprite 并且工作得很好)

public class GameMain extends Core{
public void init(){
test.setX(500); test.setY(488);
}

public void draw(Graphics2D g){
//the above code
}
}

如果它们完全相同,我不认为覆盖它们有什么意义。

最佳答案

你的问题是,当你创建一个Character时,你调用的是Spriter的无参数构造函数,而不是带有Image<的构造函数。/ 参数。这意味着你的形象永远不会被定型;这就是为什么你有空指针异常。

您可能应该将 Image 添加到 Character 的构造函数参数中,并将其传递给父类(super class)构造函数,如下所示。

public Character(Spriter s, long health, long power, Image img) {
super(img);

// and so on.

@ElliottFrisch - 如果这就是您想要通过答案表达的内容,我深表歉意。我无意重复;我只是不太确定这是否是你的意思。无论如何,这肯定是问题所在。

关于java - 仅当方法被重写时,通过扩展类调用方法才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941717/

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