Java继承问题

发布于 2024-08-28 10:43:35 字数 843 浏览 3 评论 0原文

我有一个抽象类 Airplane,以及两个类 PassengerAirplane 和 CargoAirplane,它们扩展了 Airplane 类。 我还有一个 Measurable 接口,以及两个实现它的类 - People 和 Containers。
因此,Airplane 可以自己做很多事情,并且有一种方法可以将可测量的东西添加到飞机上(称为 addAMeasurableThing)。 PassengerAirplane/CargoAirplane 和普通飞机之间的唯一区别是 addAMeasurableThing 应该只接受人员/容器,而不是任何类型的可测量事物。我该如何实施?

我尝试这样做:
飞机等级:

公共抽象飞机 addAMeasurableThing (可测量的 m, int 位置);

客机舱位:

公共飞机addAMeasurableThing (可测量的 m, int 位置) { if (m 人的实例)...

CargoAirplane 类:

公共飞机addAMeasurableThing (可测量的 m, int 位置) { if (m 容器实例)...

但是当我调试它时,我注意到 CargoAirplane 类中的 addAMeasurableThing 永远不会被调用,因为这两个方法具有相同的签名。那么,如何根据传递的 Measurable 事物的类型来调用适当的 PassengerAirplane/CargoAirplane 的 addAMeasurableThing 呢?

谢谢!

I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend class Airplane.
I also have an interface Measurable, and two classes that implement it - People and Containers.
So, Airplane can do many things on its own, and there is a method which allows measurable things to be added to the airplane (called addAMeasurableThing). The only difference between PassengerAirplane/CargoAirplane and just an Airplane is that addAMeasurableThing should only accept People / Containers, and not any kind Measurable things. How do I implement this?

I tried doing:
Airplane class:

public abstract Airplane
addAMeasurableThing (Measurable m, int
position);

PassengerAirplane class:

public Airplane addAMeasurableThing
(Measurable m, int position) { if (m
instanceof People)...

CargoAirplane class:

public Airplane addAMeasurableThing
(Measurable m, int position) { if (m
instanceof Containers)...

But when I was debugging it, I've noticed that addAMeasurableThing in the CargoAirplane class never gets called, because both methods have the same signature. So how can the appropriate PassengerAirplane/CargoAirplane's addAMeasurableThing be called, depending on the type of Measurable thing that is being passed on?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯狂的代价 2024-09-04 10:43:35

使用泛型:

abstract class Airplane<M extends Measurable>{
    public abstract Airplane addAMeasurableThing(M m, int position);
}

class PassengerAirplane extends Airplane<People>{
    @Override
    public Airplane addAMeasurableThing(People p, int position{ ... }
}

Use generics:

abstract class Airplane<M extends Measurable>{
    public abstract Airplane addAMeasurableThing(M m, int position);
}

class PassengerAirplane extends Airplane<People>{
    @Override
    public Airplane addAMeasurableThing(People p, int position{ ... }
}
早茶月光 2024-09-04 10:43:35

尝试考虑使您的 Measurable 类通用,然后使用例如 MeasurableMeasurable

Try looking into making your Measurable class generic, and then use e.g. Measurable<People>, Measurable<? extends Foo>, etc.

别忘他 2024-09-04 10:43:35

决定调用哪个的不是参数类型,而是对象类型。因此,这取决于您是否创建了 PassangerPlane 或 CargoPlane 或将其传递给调用 addAMeasurableThing 的方法。这是一个更大的设计问题,即如何确保只有正确类型的可衡量对象才能进入飞机。例如,如果添加了错误的可测量值,您可能会抛出异常,然后进一步调用堆栈通知用户问题。这取决于决定需要传递给飞机的可测量对象的因素。

您可能还需要 Airplane 上的静态方法(或新类 AirplaneFactory),它根据您传递的示例 Mesurable 为您返回正确的子类。

It isn't the parameter type that determines which is called, it is the object type. So it depends if you created or passed in a PassangerPlane or a CargoPlane to the method that calls addAMeasurableThing. It is a larger design question of how you ensure that you only have the right type of Measurables to go into the plane. You might, for example, just throw an exception if the wrong measurable is added and then further up the call stack inform the user of the problem. It depends on what is determining the Measurable objects that need to be passed to the Airplane.

You also might want a static method on Airplane (or a new class AirplaneFactory) which gives you back the right subclass depending on a sample Mesurable that you pass it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文