Java继承问题
我有一个抽象类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用泛型:
Use generics:
尝试考虑使您的 Measurable 类通用,然后使用例如
Measurable
、Measurable
等Try looking into making your Measurable class generic, and then use e.g.
Measurable<People>
,Measurable<? extends Foo>
, etc.决定调用哪个的不是参数类型,而是对象类型。因此,这取决于您是否创建了 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.