0

ClassCastException在一些类 MarketplaceBasicProductList和接口IMarketplaceProductList类之间进行转换时得到了 - 为什么?

public static IMarketplaceProductList createMarketplaceBasicProductList(Table basicInfoTable) {
        ArrayList list = new ArrayList();
        if (basicInfoTable != null) {
            for (int i = 1; i <= basicInfoTable.getRowCount(); i++) {
                IMarketplaceBasicProduct product = createMarketplaceBasicProduct(basicInfoTable, i);
                list.add(product);
            }
        }
        return (IMarketplaceProductList) new MarketplaceBasicProductList(list);
    }

其他细节是:

1.

 public interface IMarketplaceProductList extends Serializable {

    public int getProductCount();
    public IMarketplaceBasicProduct getProductMKPInfo(int index);

}
  1. public class MarketplaceBasicProductList extends AbstractProductList{ ...................

  2. public abstract class AbstractProductList implements IProductList{ ......

我需要在这里添加还实现 IMarketplaceProductList 吗?

  1. public interface IMarketplaceBasicProduct extends Serializable{ ...获取 MarketplaceBasicProduct 函数的方法..

5.public class MarketplaceBasicProduct extends BasicProduct implements IMarketplaceBasicProduct, IGenericProductFields {

  1. public class BasicProduct extends AbstractProduct {

7.public abstract class AbstractProduct extends AbstractGenericProduct implements IProduct {

我还需要在这里添加实现IMarketplaceBasicProduct吗?

4

1 回答 1

2

你得到,ClassCastException因为你的班级MarketplaceBasicProductList 不是 IMarketplaceProductList

MarketplaceBasicProductList extends AbstractProductList implements IMarketplaceProductList 

从您拥有的结构来看,AbstractProductList对于每种类型的产品列表都是通用的,并且IMarketplaceProductList将包含特定的方法MarketplaceBasicProductList,因此只会MarketplaceBasicProductList实现IMarketplaceProductList

此外,我觉得IMarketplaceProductList将提供以上及以上的功能,IProductList因此它的IMarketplaceProductList定义应该是:

public interface IMarketplaceProductList extends IProductList {

}
于 2012-06-01T06:34:06.223 回答