Java仅调用以parent作为函数参数的方法

标签 java function call parent abstract

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

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 同时包含镜头和敌人,但由于某种原因,碰撞只调用 collidesWith(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仅调用以parent作为函数参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65064826/

相关文章:

excel - 如何在 VBA excel 中运行/调用另一个模块中的模块?

javascript - [].forEach.call(...?

java - 不创建表的Spring Hibernate

java - 如何正则表达式不包含空格的单词?

javascript - 如何通过 jQuery 中的两个函数正确传递两个对象

python - 当以 f(x)(y) 格式调用 Python 函数时,这意味着什么?

javascript - 如何在 Firebase 身份验证函数中调用函数?

java - Ejb 3,消息驱动bean与有状态 session bean合作?

java - 如何将带有 '/' 符号的值作为获取参数传递给我的 Controller ?

c++ - 从 C++ 中的类访问私有(private)变量