接口期间的java classcastexception和一些类的转换

时间:2012-06-01 06:28:51

标签: java

我在某些课程中说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 {.............

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

    1. public interface IMarketplaceBasicProduct extends Serializable {...获取MarketplaceBasicProduct函数的方法..
    2. 5。public class MarketplaceBasicProduct extends BasicProduct implements IMarketplaceBasicProduct, IGenericProductFields {

      1. public class BasicProduct extends AbstractProduct {
      2. 7。public abstract class AbstractProduct extends AbstractGenericProduct implements IProduct {

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

1 个答案:

答案 0 :(得分:2)

由于您的课程ClassCastException 不是 MarketplaceBasicProductList,您将获得IMarketplaceProductList

MarketplaceBasicProductList extends AbstractProductList implements IMarketplaceProductList 

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

此外,我觉得IMarketplaceProductList将在IProductList之上和之上提供功能,因此IMarketplaceProductList定义应该是这样的:

public interface IMarketplaceProductList extends IProductList {

}