【问题标题】:Java Polymorphism Subclass FunctioncallJava多态子类函数调用
【发布时间】:2020-09-04 11:50:52
【问题描述】:

我很确定我可以在 Stackoverflow 上找到这个问题的答案。不幸的是,我不知道这样做的具体公式。

鉴于以下代码我有问题,我想避免类型检查。 cmets 可能会比我的话更好地描述它。

现在我正在尝试创建一个形状系统,其中每个形状都可以与每个可能的特定形状发生碰撞。 碰撞类:

public class ShapeCollision {
    public static boolean intersects(RectShape rectShape1, RectShape rectShape2) { return true; }

    public static boolean intersects(LineShape lineShape, RectShape rectShape) { return true; }

    public static boolean intersects(RectShape rectShape1, Shape shape) { return true; }

    public static boolean intersects(LineShape lineShape, Shape shape) { return true; }

    public static boolean intersects(Shape shape1, Shape shape2){ return true; }
}

形状类:

public class RectShape extends Shape {
    Vector size;
    public RectShape(Vector pos, Vector size) {
        super(pos);
        this.size = size;
    }
    @Override
    public boolean intersects(IShape shape) {
        return ShapeCollision.intersects(this, shape);
    }
}

public class LineShape extends Shape {
    Vector pos2;
    public LineShape(Vector pos, Vector pos2) {
        super(pos);
        this.pos2 = pos2;
    }
    @Override
    public boolean intersects(IShape shape) {
        return ShapeCollision.intersects(this, shape);
    }
}

public class Shape implements IShape {
    protected Vector pos;
    public Shape(Vector pos) {
        this.pos = pos;
    }
    @Override
    public Vector getPos() {
        return pos;
    }
    @Override
    public void setPos(Vector pos) {
        this.pos = pos;
    }
    @Override
    public void move(Vector movementAmount) {
        pos.add(movementAmount);
    }
    @Override
    public boolean intersects(IShape shape) {
        return ShapeCollision.intersects(this, shape);
    }
}

这对我来说是令人困惑的部分:

Shape rect = new RectShape(new Vector(0,0), new Vector(20,20));
Shape rect2 = new RectShape(new Vector(0,0), new Vector(20,20));
Shape line = new LineShape(new Vector(0,0), new Vector(20,20));

//Since I am saving shape and no specific shapetype, it will pass shape and pick the specific superFunction 
//Right now it calls the intersects(RectShape rectShape1, Shape shape) function due to calling it through the shape variable
rect.intersects(rect2); 
//This calls the intersects(LineShape lineShape, Shape shape) function
rect.intersects(line);    
//This calls the intersects(Shape shape1, Shape shape2) function
ShapeCollision.intersects(rect, line);

如何在不指定变量类型的情况下实现它,即调用带有子类参数的“正确”函数。 (例如:(LineShape lineShape,RectShape rectShape))

我不想在这些函数中进行任何类型检查并专门调用这些函数,而是尽可能使用一些 DesignPatters 或类似的东西:)

【问题讨论】:

  • 如果我没记错的话 Shape 类实现了 IShape 和所有其他类 LineSHape 和 RactShape 扩展了 Shape 类。正确吗?
  • @GauravJeswani 你是对的。您还可以就我应该使用的类结构提出建议。我只是好奇这是否可能。
  • 如果是这样的话 ShapeCollision.intersects(this, shape);正在编译?
  • 为了回答你的问题,我会调整代码,让一切都可见。我不确定你在问什么。我想你可能误解了一些东西;)
  • 是的,请更新代码。

标签: java polymorphism subclass


【解决方案1】:

如果没有在函数内部进行一些类型检查或在将 Shape 实例传递给函数调用之前对它们进行一些显式转换,则无法实现您想要的。

当然你可以用特定的类来声明对象引用,但我想这并没有什么帮助。

【讨论】:

  • 感谢您的信息。也许有人会找到办法。但从这个角度来看,你会以同样的方式设计架构吗?保存特定形状的问题是我必须为每个形状创建一个 GameObject 类。我考虑过创建一个名为“LineObjects”的类,其中对象由简单的线条和圆形结构组成。在那种情况下,我只有这么多类型检查要做。
  • 老实说,我并没有真正了解您的课程的设计或逻辑,那将是另一个话题。当然,我对您要执行的操作的了解非常肤浅,但是您可以尝试的是使用泛型参数并使用它们来调用正确的函数。像这样: public static boolean intersects(S1 shape1, S2 shape2){ 在这里你做所有的类型检查,看看调用哪个方法 } 这样你可以将单个公共函数和所有其他的 instersects 方法设为私有。增加了一些开销。
  • 我要补充一点,如果你想让这个方法成为静态的(就像你在 ShapeCollision 类中所做的那样),那么从对象实例调用这个方法没有多大意义。您可以将其设为 Shape 类的方法(静态或非静态)或 ShapeCollision 类的方法(我不想讨论哪个更好)。
  • 我会尝试使用泛型。也许这会有所帮助。仍然是一个有用的评论:) @Dreddy-ce 我是从另一个类调用它,所以我必须做更少的类型检查,因为它是用“正确”的第一个参数调用的,它是子类的。所以它用 RectShape、Shape 而不是 Shape、Shape 来调用它
猜你喜欢
  • 1970-01-01
  • 2015-06-09
  • 2011-12-17
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
相关资源
最近更新 更多