1

我正在处理一项任务,要求我创建几个类的两个通用集合(称为 ComputerOrder 和 PartyTrayOrder):ComputerPart、Peripheral 和 Service,以及 Cheese、Fruit 和 Service。所有这些类(ComputerPart、Fruit、Service 等)都扩展了一个名为 Product 的类。

我遇到的问题是在Java中绑定多个类似乎是不可能的,因为你可能只绑定多个接口,并且不允许我修改现有的类结构。

我已经阅读并看到它建议我创建和接口“在”所有相关类中“编码”,但是我看不到如果没有 ComputerPart、Peripheral 等也作为接口然后使用 extend 关键字,这怎么可能。我已经看到它进一步建议我创建名为 ComputerProduct 和 PartyTrayProduct 的抽象类,但我看不出这将如何以任何方式限制类型。

我不允许发布图像,但这是一个 类图,包括一些不相关的分配对象

这是我当前的 ComputerOrder 代码,尽管它只限制了父类产品,这意味着它并不是真正的赋值指定。

public class ComputerOrder<T extends Product> extends GenericOrder<T> {
    public ComputerOrder() {
        super();
    }
}

以下是分配规范:

  1. 设计并实现一个称为 GenericOrder 的通用容器,它充当 Products.java 中任意数量的对象的集合。设计一种机制,为容器的每个实例提供唯一标识符。您必须使用 Java 泛型特性。

  2. 设计和实现一个名为 ComputerOrder 的 GenericOrder 子类,它采用任意数量的不同类的 ComputerPart 对象、Peripheral 对象和 Service 对象。根据需要实施尽可能多的方法。

  3. 设计并实现一个名为 PartyTrayOrder 的 GenericOrder 子类,它采用任意数量的不同类别的 Cheese 对象、Fruit 对象和 Service 对象。根据需要实施尽可能多的方法。(...)

4

2 回答 2

2

这是我想出的唯一解决方案,它在分配给这封信之后仍然有意义(尽管我认为这不是一个“好”的解决方案 - 请参阅下面的注释):ComputerOrderandPartyTrayOrder可以提供接受以下特殊类型Product

abstract class Product {}
class ComputerPart extends Product {}
class Peripheral extends Product { }
class Cheese extends Product {} 
class Fruit extends Product {}
class Service extends Product {} 

abstract class GenericOrder<T extends Product> {
    protected final void addImpl(T t) {
    }
}

class ComputerOrder extends GenericOrder<Product> {
    void add(ComputerPart t) {
        addImpl(t);
    }
    void add(Peripheral t) {
        addImpl(t);
    }
    void add(Service t) {
        addImpl(t);
    }
}

class PartyTrayOrder  extends GenericOrder<Product> {
    void add(Cheese t) {
        addImpl(t);
    }
    void add(Fruit t) {
        addImpl(t);
    }
    void add(Service t) {
        addImpl(t);
    }
}

这样,订单实现可以完全接受正确的类型:

public class ProductExample {

    public static void main(String[] args) {
        ComputerOrder c = new ComputerOrder();
        c.add(new ComputerPart());
        c.add(new Peripheral());
        //c.add(new Cheese()); // Not allowed
        //c.add(new Fruit()); // Not allowed
        c.add(new Service());

        PartyTrayOrder p = new PartyTrayOrder();
        //p.add(new ComputerPart());  // Not allowed
        //p.add(new Peripheral());  // Not allowed
        p.add(new Cheese());
        p.add(new Fruit());
        p.add(new Service());

    }
}

我认为这是预期的解决方案,因为作业包含广泛的提示:

根据需要实施尽可能多的方法。

因此,最有可能的目标不是实现一种神奇地绑定到多个类的方法。相反,必须为每个“类别”添加一种方法。


顺便说一句:我的直觉是这种设计可以改进。想象一下,您必须为新类型的订单创建类等CarOrderFahsionOrder或者想象一下,PartyTrayOrder必须将其扩展为也处理类似Meator Dipor的类Salad:你最终会得到几十个类和几十个专门的方法。

这一切都可以通过引入与特定“订单类型”可接受的类型完全匹配的专用“产品类型”来避免。所以我认为 (Product应该是一个开始的接口,并且) 应该有类似ComputerProductand的接口PartyTrayProduct,如

interface Product {}

interface ComputerProduct extends Product {}
class ComputerPart implements ComputerProduct  {}
class Peripheral implements ComputerProduct  {}

interface PartyTrayProduct extends Product {}
class Cheese implements PartyTrayProduct{} 
class Fruit implements PartyTrayProduct{}

class Service implements Product {} 
class DeliveryService implements PartyTrayProduct{} 
class AssemblyService implements ComputerProduct  {}

这样,特定的所需边界ComputerOrderPartyTrayOrder已经使用类层次结构建模。这样做的好处是:你不再需要ComputerOrderandPartyTrayOrder类了!然后GenericOrder可能是非抽象的,为了创建特定类型的订单,您只需正确使用泛型类型绑定。

这是一个完整的示例,我刚刚Salad将其作为一个新的PartyTrayProductCarPart作为一种新类型的产品投入使用,而无需扩展或修改任何“基础设施类”:

interface Product {}

interface ComputerProduct extends Product {}
class ComputerPart implements ComputerProduct  {}
class Peripheral implements ComputerProduct  {}

interface PartyTrayProduct extends Product {}
class Cheese implements PartyTrayProduct{} 
class Fruit implements PartyTrayProduct{}

class Service implements Product {} 
class DeliveryService implements PartyTrayProduct{} 
class AssemblyService implements ComputerProduct  {}

class Salad implements PartyTrayProduct{} // A new PartyTrayProduct

// Entirely new product type:
interface CarProduct extends Product {}
class CarPart implements CarProduct  {}
class CarInterior implements CarProduct  {}

class GenericOrder<T extends Product> {
    public void add(T t) { }
}

public class ProductExample2 {
    public static void main(String[] args) {

        GenericOrder<ComputerProduct> c = new GenericOrder<ComputerProduct>();
        c.add(new ComputerPart());
        c.add(new Peripheral());
        //c.add(new Cheese()); // Not allowed
        //c.add(new Fruit()); // Not allowed
        c.add(new AssemblyService());

        GenericOrder<PartyTrayProduct> p = new GenericOrder<PartyTrayProduct>();
        //p.add(new ComputerPart());  // Not allowed
        //p.add(new Peripheral());  // Not allowed
        p.add(new Cheese());
        p.add(new Fruit());
        p.add(new Salad()); // Just add it...
        p.add(new DeliveryService());

        // Easy to extend:
        GenericOrder<CarProduct> e = new GenericOrder<CarProduct>();
        e.add(new CarPart());
        e.add(new CarInterior());
    }
}
于 2019-06-17T12:00:07.833 回答
0

该图准确地向您展示了类层次结构的相似之处。每个粗箭头都是一个类扩展。当箭头从 A 指向 B 时,这意味着 A 扩展了 B。您只需将其关闭到 java 代码中。

当 A 扩展 B 并且 B 扩展 C 时,A 是 C。我认为这里不需要多基继承。

于 2019-06-16T20:37:45.567 回答