仅Java调用的方法,其函数参数为父级

仅Java调用的方法,其函数参数为父级,java,function,call,parent,abstract,Java,Function,Call,Parent,Abstract,在我的项目中,我遇到了这个问题,我有一个抽象的实体类,它的子类是玩家、射击和敌人。我想检查它们之间有无碰撞。一个单独的物理课程正在使用以下代码进行碰撞评估: public class Physics { private static int height = 32; private static int width = 32; public static void collision(Entity entity, LinkedList<Entity> eL

在我的项目中,我遇到了这个问题,我有一个抽象的实体类,它的子类是玩家、射击和敌人。我想检查它们之间有无碰撞。一个单独的物理课程正在使用以下代码进行碰撞评估:

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));
             }
         }
     }
}
玩家:

   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;
}
射门

敌人


如何让它调用正确的函数?

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

公共抽象类实体{
保护双x;
保护双y;
公共实体(双x,双y){
这个.x=x;
这个。y=y;
}
公共摘要无效勾号();
公共双getX(){return x;}
公共双getY(){return y;}
公共矩形getBounds(整数宽度、整数高度){
返回新矩形((int)x,(int)y,宽度,高度);
}
公开摘要无效(T e);
}

可能是错误的,我对回答有关stackoverflow的问题还不熟悉

希望这能让你明白一些:

entity.collizeswith(eList.get(i))

eList.get(i)在那一行中,在你的物理类中,Entity返回一个Entity类型的对象。 这是因为它的定义如下:

LinkedList eList

这意味着,如果有一个重载接受该实体,它将只调用该方法。这正是我在你的代码中看到的。您有一个参数为Entity的“CollizeSwith”的方法重载。 在实体的所有子类中


我认为您应该阅读更多关于“Java多态性”的内容。

我认为除了Jamie Bisotti指定的泛型类型之外,还有一种解决方案,就是使用接口和开关来检查哪个类是什么。
这是一个接口,它声明了所有可能发生冲突的实体必须具有的方法:

公共接口可冲突{
公共布尔碰撞开关(可碰撞实体);
}
然后,您希望能够碰撞的每个类都必须实现:

公共类实体实现可碰撞{
私人整数速度;
公敌(双x,双y){
super(x,y);
随机=新随机();
速度=随机。下一个(3)+1;
}
公共空白勾号(){
x-=速度;
}
@凌驾
公共空间碰撞开关(可碰撞e){
如果(e.getClass().equals(Shot.class)){
//做点什么,我撞上了一枪
}else如果(e.getClass().equals(敌方类)){
//我正在与敌人相撞
}
等
}
我更喜欢使用接口,这样我就可以指定每个行为。目前,它似乎很简单,所有内容都可以从实体抽象类扩展,但有时您会通过许多其他功能区分每个实体。
例如,飞行的敌人、行走的敌人、ecc ecc,您可以使用接口指定每个功能

在这种情况下,接口也很简单,但是您可以指定许多要实现的方法,例如

public boolean canCollide();
public boolean isAlive(); //if the entity is already dead you might want not to stop a bullet
public boolean isAnimatingDeath(); //if the entity is animating death could collide with another antity because of its exploding animation, maybe you want to avoid that.

您可以在抽象实体类中实现som方法,但该抽象实体不应该知道其子实体。这就是为什么要直接在子实体中实现一些使用泛型“可碰撞”类型作为输入的方法。

对于我来说,实体类了解敌人和射击是错误的

为什么不从实体和子类中删除这两个方法:

public abstract void collidesWith(Enemy e);
public abstract void collidesWith(Shot s);
并仅保留和实施:

public abstract void collidesWith(Entity e);

如果您需要知道作为参数传递的实体e的类型,您可以使用反射,但这是一种糟糕的设计。最好使用实现collidesWith,这样它就不需要知道传递参数的确切类型。

请提供您提到的每个类的声明。我喜欢这个解决方案,但我会使用我在回答中指定的接口。+1谢谢。我的目标不是解决他的问题,就像你的全面回答一样。主要是指代码为什么只执行这些方法。@OminousApple非常欣赏所有答案^polymorphics是我的第一个想法,每个类只有CollizeSwith(),具有不同的参数类(例如collidesWith(Shot s),collidesWith(Foreign e))。但我还必须具有基本的collidesWith(实体e),因为它是一个抽象函数,我认为它对于物理循环是必要的。结果是只有cW(实体e)在每个类中调用,而不是在具有匹配参数类型的类中调用。我能做什么?让我这样说。被碰撞的对象将始终具有实体的基类型。(在当前情况下,我可以在代码中看到)您不需要其他方法重载。因为`CollizeSwith(实体e)`将处理所有这些。因此,您将在
碰撞(实体e)
中处理对象射击。您必须找出如何检查该属性e是否为Shot、Player、Bullet(从实体派生的特定类型)。一种肮脏的检查方法是在该方法中使用instanceOf。谢谢你的回答!遗憾的是,我担心它现在对我不起作用。它与我几个小时前的检查方法类似,但在与我的上级讨论后,他说这不是一种正确的面向对象方法,因为“如果”中的类型检查-s、 我知道我给出的示例代码也很不正确,但这就是我的代码的当前状态,我想知道从这里可以走到哪里。
public boolean canCollide();
public boolean isAlive(); //if the entity is already dead you might want not to stop a bullet
public boolean isAnimatingDeath(); //if the entity is animating death could collide with another antity because of its exploding animation, maybe you want to avoid that.
public abstract void collidesWith(Enemy e);
public abstract void collidesWith(Shot s);
public abstract void collidesWith(Entity e);