【问题标题】:Java Generics AssignmentJava 泛型分配
【发布时间】:2010-12-18 05:02:18
【问题描述】:

我有一个关于 Java 泛型的作业问题。给定的类是 Product.java。它有抽象类 Product,然后 ComputerPart、Peripheral、Fruit、Cheese 和 Service 扩展 Product。

  1. 仔细研究 Products.java 中的类结构。
  2. 设计一个名为 GenericOrder 的通用容器,它充当 Products.java 中任意数量对象的集合。设计一种机制,为容器的每个实例提供唯一标识符。根据需要实施尽可能多的方法。您必须使用 Java 泛型功能。
  3. 设计和实现一个名为 ComputerOrder 的 GenericOrder 子类,它采用任意数量的不同类的 ComputerPart 对象、Peripheral 对象和 Service 对象。根据需要实施尽可能多的方法。
  4. 设计并实现一个名为 PartyTrayOrder 的 GenericOrder 子类,它采用任意数量的不同类别的 Cheese 对象、Fruit 对象和 Service 对象。根据需要实施尽可能多的方法。
  5. 设计并实现一个名为 OrderProcessor 的类。您必须至少实现以下方法:

    accept; // this method accepts a GenericOrder or any of its subclass objects and stores it in any internal collection of OrderProcessor.
    
    
    process; // this method sorts all accepted orders in the internal collection of GenericOrder into collections of ComputerPart, Peripheral, Cheese, Fruit, and Service. You must associate each object with the unique identifier. You may refer to the TwoTuple.java example in the text book.
    
    
    dispatchXXX; // this method simulates the dispatch of the sorted collections. For example, the method dispatchComputerParts() should produce this output:
    
    
    Motherboard  name=Asus, price=$37.5, order number=123456
    
    
    Motherboard  name=Asus, price=$37.5, order number=987654
    
    
    RAM  name=Kingston, size=512, price=$25.0, order number=123456
    
    
    You may overload each of the above methods as you think necessary.
    
  6. 创建一个客户端类来测试 OrderProcessor。您将需要创建一个数据生成器用于测试目的。这不是强制性的,但您可以使用 TIJ 第 637 至 638 页中的数据生成器的变体。

  7. 将上述代码打包成一个 zip 文件并发送给您的导师。

我已经完成了第 1 个要求,但我无法理解第 2 个要求。这是我做的代码:

public class GenericOrder<T> {
    private static long counter = 1;
    private final long id = counter++;
    private List<T> products;
    private T theClass;

    public GenericOrder() 
    {
        products = new ArrayList<T>();
    }

    public long getId() {
        return id;
    }

    public void addProduct(T newProduct) {
        products.add(newProduct);

    }

    public int getNumberOfProducts() {
        return products.size();
    }

    public List<T> getProducts()
    {
        return products;
    }

    public void setProducts(List<T> products)
    {
        this.products = products;
    }

    public T get()
    {
        return theClass;
    }

    public void set(T theClass)
    {
        this.theClass = theClass;
    }
}
public class ComputerOrder<T> extends GenericOrder<T> {
    private List<T> products; 
    private T theClass;

    public ComputerOrder() 
    {
        products = new ArrayList<T>();
    }


    public void addProduct(T newProduct) {
        products.add(newProduct);

    }

    public int getNumberOfProducts() {
        return products.size();
    }

    public List<T> getProducts()
    {
        return products;
    }

    public void setProducts(List<T> products)
    {
        this.products = products;
    }

    public T get()
    {
        return theClass;
    }

    public void set(T theClass)
    {
        this.theClass = theClass;
    }
}

请在这里帮助我,看看是否有人比我更了解第二个要求。 谢谢大家。

【问题讨论】:

    标签: java generics


    【解决方案1】:

    我认为第二部分正在寻找一个具有上限类型限制的类,所以可能是这样的:

    public class ComputerOrder<T extends Product> extends GenericOrder<T> {
        // etc...
    }
    

    编辑

    我错过了水果和奶酪课程。为了使类型边界起作用,您很可能需要将您的计算机产品编码为接口并使用以下内容:

    public class ComputerOrder<T implements MyInterface> extends GenericOrder<T> {
        // etc...
    }
    

    有关有界类型参数的更多信息,请阅读here

    【讨论】:

    • 如果 T 只扩展 Product,那么它为什么要专门询问 ComputerOrder,它接受任意数量的不同类的 ComputerPart 对象、Peripheral 对象和 Service 对象
    • @Jack:请注意,您的 GenericOrder 类接受任何类型。但是,ComputerOrder 只允许扩展您的 Product 类的类型。由于 Product 是抽象的,因此 Product 类本身不能被实例化。它只能使用扩展它的类型来初始化。
    • 感谢您的回复,泰勒。最后一个问题,如果 Product.java 也有 Fruit and Cheese,它也扩展了 Product,但是 ComputerOrder 不采用 Fruit and Cheese 对象。我该怎么办?
    • 不尊重,我认为我不需要在这里创建接口。这是一个课程作业,我想如果它要求一个接口,它会给我提示。但我会记住你所说的。
    • 我没有看到任何其他可行的方法来限制类型。 Cheese 类和 ComputerPart 类彼此非常不同。我不确定作业的指导方针到底是什么,但是拥有描述类似产品的接口或抽象类是有意义的,即 ComputerProduct 类和 FoodProduct 类,它们进一步扩展了 Product 类,或类似的东西自然。
    【解决方案2】:

    Tyler 打败了我,但我只是想补充一点,您不需要重写所有这些方法......它们已经在基类中为您提供了。

    您应该添加的是ComputerParts和Peripherals等通用的方法。

    【讨论】:

      【解决方案3】:

      试试这个:

      interface CO{} //for computer orders 
      interface PTO{} //party tray orders
              
      class ComputerPart implements CO{} 
      class Peripheral implements CO{}      
      class Fruits implements PTO{} 
      class Cheese implements PTO{}     
      class Service implements CO, PTO{}
      public static class ComputerOrders<T extends CO> extends GenericOrder{}  
      public static class PartyTrayOrder <T extends PTO> extends GenericOrder {}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多