【发布时间】:2014-11-18 19:29:55
【问题描述】:
我有几门课。通用订单从产品类中的事物中收集,这些事物只是一些对象,例如水果和计算机零件。计算机订单扩展了 GenericOrder,它使用 Product 类中的一些对象创建自己的集合,因此计算机订单有自己的集合,称为 computerOrder。现在有第三个集合,称为 orderProcessor,我认为队列的使用非常好,它采用先进先出的方式。
将 genericOrder 和 computerOrder 添加到 orderProcessor 的最佳方法是什么?将 computerOrder 添加到 orderProcessor 时出现错误。有没有更好的方法或者我在正确的轨道上?
import java.util.*;
public class GenericOrder <T extends Product> {
List<Product> genericOrder;
public void compPrice(float comPrice){
genericOrder.add(new ComputerPart(comPrice));
Product a = genericOrder.get(0);
}
}
import java.util.*;
public class ComputerOrder extends GenericOrder<Product> {
ArrayList<Product> computerOrder;
public void addDrive (String dType, int dSpeed, float price) {
computerOrder.add(new Drive(dType ,dSpeed ,price));
}
import java.util.ArrayList;
public class OrderProcessor extends GenericOrder<Product>{
ArrayList<Product> orderProcessor;
protected int queSize=0;
protected int front=0, rear;
public void accept()// this method accepts a GenericOrder or any of its subclass objects and stores it in any internal collection of OrderProcessor.
{
orderProcessor.addAll(genericOrder);
orderProcessor.addAll(genericOrder.computerOrder);
orderProcessor.addAll(partyTrayOrder);
}
【问题讨论】:
-
只是好奇,为什么订单处理器是从通用订单扩展而来的。为什么不定义一个称为通用订单的接口并让每个特定订单实现它。然后,订单处理器接受项目通用订单列表并处理它们。
-
genericOrder是List,因此它没有computerOrder成员。但是,在调查了这个之后,我无法弄清楚你真正想要做什么。 -
@user3586195 如果我将 GenericOrder 类设为接口,那么我将不得不告诉计算机订单以获取其所有方法,甚至奶酪和水果。不属于那里。
-
@ajb 我厌倦了把它变成一个 ArrayList 。 orderProcessor 仍然不接受存储在 computerOrder 中的对象。我试图将作为集合的通用订单和计算机订单的集合添加到另一个名为 orderProcessor 的集合中
-
ArrayList也没有computerOrder成员。 Java 定义的所有类都没有computerOrder成员。.computerOrder只能应用于ComputerOrder对象。但是什么对象?这是我想不通的。
标签: java collections queue