【问题标题】:Copying collection A to collection B with including collections A's sub classes ? For taking online orders. Java将集合 A 复制到集合 B 并包含集合 A 的子类?用于接受在线订单。爪哇
【发布时间】: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);



}

【问题讨论】:

  • 只是好奇,为什么订单处理器是从通用订单扩展而来的。为什么不定义一个称为通用订单的接口并让每个特定订单实现它。然后,订单处理器接受项目通用订单列表并处理它们。
  • genericOrderList,因此它没有 computerOrder 成员。但是,在调查了这个之后,我无法弄清楚你真正想要做什么。
  • @user3586195 如果我将 GenericOrder 类设为接口,那么我将不得不告诉计算机订单以获取其所有方法,甚至奶酪和水果。不属于那里。
  • @ajb 我厌倦了把它变成一个 ArrayList 。 orderProcessor 仍然不接受存储在 computerOrder 中的对象。我试图将作为集合的通用订单和计算机订单的集合添加到另一个名为 orderProcessor 的集合中
  • ArrayList 也没有 computerOrder 成员。 Java 定义的所有类都没有computerOrder 成员。 .computerOrder 只能应用于 ComputerOrder 对象。但是什么对象?这是我想不通的。

标签: java collections queue


【解决方案1】:

也许你看起来像这样:

public class Order<T extends Product> {
    List<T> products;

    public void add(T productItem) {
    }
}

public class ComputerOrder extends Order<ComputerPart> {
    //I think, you don't need collection of ComputerParts here, cause you have it in the parent class
    //Maybe you either don't need method like addDrive — you can use add(new Drive()) instead
}

//It's better to prefer composition over inheritance
public class OrderProcessor {
    List<Product> products;

    public void accept(List<? extends Product> products) {
        this.products.addAll(products);
    }
}

您可以通过这种方式使用 OrderProcessor:

OrderProcessor orderProcessor = new OrderProcessor();
orderProcessor.accept(new ArrayList<Product>());
orderProcessor.accept(new LinkedList<ComputerPart>());
orderProcessor.accept(new CopyOnWriteArrayList<SomethingExtendingProduct>());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多