gpt4 book ai didi

Java 只调用父作为其函数参数的方法

转载 作者:行者123 更新时间:2023-12-04 08:30:10 25 4
gpt4 key购买 nike

在我的项目中,我遇到了这个问题,我有一个抽象类 Entity,它的 child 是 Player、Shot 和 Enemy。我想检查它们之间的碰撞。一个单独的 Physics 类正在使用以下代码进行碰撞评估:

public class Physics {

private static int height = 32;
private static int width = 32;

public static void collision(Entity entity, LinkedList<Entity> eList) {
for (int i = 0; i<eList.size(); i++) {
if (entity.getBounds(width, height).intersects(eList.get(i).getBounds(width, height))) {
entity.collidesWith(eList.get(i));
}
}
}
}
LinkedList 包含 Shots 和 Enemies,但由于某种原因,碰撞只调用了 collidesWith(Entity entity) 方法,而不是 collidesWith(Shot b) 或 collidesWith(Enemy e)。
编辑:提到的类(只有我认为在这种情况下对它们很重要的代码)
实体:
public abstract class Entity {

protected double x;
protected double y;

public Entity (double x, double y) {
this.x = x;
this.y = y;
}

public abstract void tick();

public double getX() { return x; }
public double getY() { return y; }

public Rectangle getBounds(int width, int height) {
return new Rectangle((int)x, (int)y, width, height);
}

public abstract void collidesWith(Entity e);
public abstract void collidesWith(Enemy e);
public abstract void collidesWith(Shot s);
球员:
   public class Player extends Entity {

private boolean alive;

private int gameWidth, gameHeight;
private GameController gCont;
private Textures textures;

public Player( String name, int x, int y, int gameWidth, int gameHeight, Textures textures, GameController gCont) {
super(x,y);
this.name = name;
this.score = 0;
this.gameWidth = gameWidth;
this.gameHeight = gameHeight;
this.gCont = gCont;
this.textures = textures;
this.alive = true;
}

public void tick() {
gCont.collisionCheck(this);
}

public void collidesWith(Enemy e) {
System.out.println("Player collides with enemy");
this.alive = false;
}
public void collidesWith(Shot s) {
return;
}

public void collidesWith(Entity e) {
System.out.println("collided with entity");
return;
}
射击
    public class Shot extends Entity {

private Textures textures;
private GameController gCont;

public Shot(double x, double y, Textures textures, GameController gCont) {
super(x, y);
this.textures = textures;
this.gCont = gCont;
}

public void tick() {
x+=10;
gCont.collisionCheck(this);

}

public void collidesWith(Entity e) {
return;
}

public void collidesWith(Enemy e) {
gCont.removeEntity(e);
gCont.removeEntity(this);
gCont.addScore();
}

@Override
public void collidesWith(Shot s) {
return;
}
敌人
public class Enemy extends Entity {
private int speed;

public Enemy(double x, double y) {
super(x, y);
Random random = new Random();
speed = random.nextInt(3)+1;
}

public void tick() {
x-=speed;
}

public void collidesWith(Entity e) {
return;
}

@Override
public void collidesWith(Enemy e) {
return;

}

@Override
public void collidesWith(Shot s) {
return;
}
我怎样才能让它调用正确的函数?

最佳答案

查看 Java 的泛型。我想你可以使用这样的东西:

public abstract class Entity<T> {

protected double x;
protected double y;

public Entity (double x, double y) {
this.x = x;
this.y = y;
}

public abstract void tick();

public double getX() { return x; }
public double getY() { return y; }

public Rectangle getBounds(int width, int height) {
return new Rectangle((int)x, (int)y, width, height);
}

public abstract void collidesWith(T e);
}

关于Java 只调用父作为其函数参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65064826/

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