简体   繁体   English

如何为特殊的“多重继承”情况在Java中定义类和接口

[英]How to define classes & interfaces in Java for a special “multiple inheritance” situation

Given a standard class which I cannot change (greenfoot Actor; caveat: I am not familiar with either java or greenfoot), I want to create two subclasses which represent transportable goods and transporting means. 给定一个我不能更改的标准类(greenfoot Actor;警告:我对java或greenfoot不熟悉),我想创建两个代表可运输货物和运输方式的子类。

The problem appears when a transporting means, such as a bike, is small enough to be a transportable good. 当诸如自行车的运输工具足够小以至于成为可运输的物品时,就会出现问题。

class Transportation extends Actor {...}
class Truck extends Transportation {...}
interface Transportable {...}
class Container extends Actor implements Transportable {...}
class Bike extends Transportation implements Transportable {...}

Now if I want to define methods to load/unload goods, in class Transportation , what shall be the class of the parameter? 现在,如果我想定义装载/卸载货物的方法,则在Transportation类中,参数的类是什么?

void load (Transportable goods) {
    :
    /* not exactly sure about call of getworld(); it returns the "world" */
    World.getWorld().removeObject(goods) <- this is where an Actor is needed
    :
} 

For bookkeeping in the greenfoot world (ie, which objects are "inside the world", meaning outside of any Transportation ), I need to call a method on goods inside load which requires an Actor - and the compiler rightfully tells me that Transportable objects are not Actor s. 对于绿色世界中的簿记(即,哪些对象位于“世界内部”,意味着在任何Transportation之外),我需要对需要loadActor loadgoods调用一种方法-编译器正确地告诉我,可Transportable对象是不是Actor

But interfaces ( Transportable ) cannot inherit from classes ( Actor ). 但是接口( Transportable )不能从类( Actor )继承。

The solutions I have seen on SO say to make the top class ( Actor , in this case) have an associated interface, but what do I do when I do not control the top class? 我在SO上看到的解决方案说使顶级类(在这种情况下为Actor )具有关联的接口,但是当我不控制顶级类时该怎么办?

EDIT: added more code in load() as requested; 编辑:根据要求在load()添加了更多代码; added class Truck class Truck

You will have to shuffle around some of your classes and create new interfaces. 您将不得不重新整理一些类并创建新的接口。

class Actor {
 // Use Greenfoot Actor class instead of this one
}

interface ITransportation {

    public void load(Transportable goods);
}

class Transportation extends Actor implements ITransportation{

    @Override
    public void load(Transportable goods) {

    }
}

class Truck extends Transportation{

}

interface ITransportable {

}

class Transportable extends Actor implements ITransportable {

}
class Container extends Transportable {

}

class HybridGood implements ITransportable, ITransportation {

    private Transportation transportation;
    private Transportable transportable;

    @Override
    public void load(Transportable goods) {
        transportation.load(goods);
    }
}
class Bike extends HybridGood {

}

With this approach, you will be able to 通过这种方法,您将能够

  • Share the common functionality of Transportation and Transportable 共享运输和可运输的通用功能
    type in HybridGoods class. 输入HybridGoods类。
  • You can access Actor type in load/unload method. 您可以在加载/卸载方法中访问Actor类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM