投射列表<>覆盖Java中的内部类

时间:2014-05-19 17:29:02

标签: java inheritance inner-classes classcastexception

我有一个内部类的类,我覆盖它。这似乎工作正常。

class Car {
   public static class CarItems
   {
      public void doStuff(){ ... }
   }
}

class Honda extends Car {
   public static class CarItems extends Car.CarItems
   {
      @Override
      public void doStuff(){ ... }
   }
}

问题

那个Car类在另一个我正在覆盖的类中:

class Dealership {
   //
   // #1: Here's a list of stuff, which includes car items
   //     as defined in parent class
   //
   protected List<CarAndStuff> carsAndStuff;

   public static class CarsAndStuff {
      private List<CarItems> carItems;
      private String name;
      // ...

      //
      // #2: This only returns the items from the rest of the
      //     clutter in this class
      //
      public List<CarItems> getCarsItems() { return carsItems; }
   }

   // As defined above
   public static class CarItems { ... }
}

class HondaDealership extends Dealership {
   //
   // #3: This sub-class only cares about the items
   //
   protected List<CarItems> carItems;

   public void extractItemsFromParent() {
      List<CarItems> _items = new ArrayList<CarItems>();

      for(CarsAndStuff stuff : carsAndStuff) {
         //
         // #4: So I try to extract the items, but using my
         //     overriden method. ERROR!
         //
         carItems.addAll(carsAndStuff.getCarItems());
      }

      this.carItems = carItems;
   }

   // As defined above
   public static class CarItems extends Car.CarItems { ... }
}

希望没有太多代码可以遵循,而且一切都很直接......我得到的错误是#4 Java试图从Car.CarItems转换为Honda.CarItems。它说:

The method addAll(Collection<? extends Honda.CarItems>)
  in the type List<Honda.CarItems> 
  is not applicable for the arguments (List<Car.CarItems>)

如果Honda.CarItems是一个Car.CarItems,为什么它不允许我将List添加到List?

2 个答案:

答案 0 :(得分:1)

尝试让Dealership CarItems系列使用Generics扩展功能。然后,在HondaDealership类中,您可以适当地转换列表。

所以,你的经销商类看起来像:

public class Dealership {
    //
    // #1: Here's a list of stuff, which includes car items
    // as defined in parent class
    //
    protected List<CarsAndStuff> carsAndStuff;

    public static class CarsAndStuff {
        private List<? extends CarItems> carItems;
        private String name;

        // ...

        //
        // #2: This only returns the items from the rest of the
        // clutter in this class
        //
        public List<? extends CarItems> getCarItems() {
            return carItems;
        }
    }

    // As defined above
    public static class CarItems {
        public void doStuff() {
        }
    }
}

您的HondaDealership类看起来像:

public class HondaDealership extends Dealership {
    //
    // #3: This sub-class only cares about the items
    //
    protected List<CarItems> carItems;

    public void extractItemsFromParent() {
        List<CarItems> _items = new ArrayList<CarItems>();

        for (CarsAndStuff stuff : carsAndStuff) {
            //
            // #4: So I try to extract the items, but using my
            // overriden method. ERROR!
            //
            carItems.addAll((List<CarItems>) stuff.getCarItems());
        }

        this.carItems = carItems;
    }

    // As defined above
    public static class CarItems extends Dealership.CarItems {
        @Override
        public void doStuff() {
        }
    }
}

答案 1 :(得分:1)

这可能不是世界上最好的答案,但会完成工作:

// Instead of addAll, add each item from CarsAndStuff.getCarItems
List<CarItems> _items = new ArrayList<CarItems>();

    for (CarsAndStuff stuff : carsAndStuff) {
        List<Car.CarItems> _carItems = stuff.getCarItems());
        for (CarItems _carItem: _carItems) {

            // ** Can't cast Car.CarItems ==> Honda.CarItems? Copy it! **
           _items.add(new CarItems(_carItem));

        }
    }

总之,不是从Car.CarItems(显式或隐式)转换为Honda.CarItems,而是制作对象的副本,从而使其成为真正的Honda.CarItems。这要求您的Honda.CarItems实现一个复制构造函数,这是一个额外的步骤:

class Honda extends Car {
  public static class CarItems extends Car.CarItems
  {

     public CarItems(Car.CarItems items) {
         this.property_1 = items.property_1;
         this.property_2 = items.property_2;
         this.property_3 = items.property_3;

         // etc.
     }

     @Override
     public void doStuff(){ ... }
  }
}