演员名单<&燃气轮机;Java中的重写内部类

演员名单<&燃气轮机;Java中的重写内部类,java,inheritance,inner-classes,classcastexception,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(){ ...

我有一个类,它有一个我重写的内部类。这似乎很有效

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

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

这辆车在另一个我也要覆盖的类中:

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 { ... }
}

如果Honda.CarItems是一辆车,为什么不让我在列表中添加一个列表???

尝试让经销商CarItems系列使用泛型扩展功能。然后,在Hondadelership类中,您可以适当地铸造列表

因此,您的经销商类别将如下所示:

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() {
        }
    }
}
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:这是一张物品清单,包括汽车物品
//如父类中定义的
//
受保护的物品清单;
公共静态类CarsAndStuff{

私人列表这可能不是世界上最好的答案,但可以完成工作:

// 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));

        }
    }

“这一切都很直截了当”…不完全是。在您的代码中,您试图将
列表添加到
列表中
。您不能这样做,因为
汽车.CarItems
不一定是
本田.CarItems
,更具体地说,
汽车.CarItems
不扩展
本田.CarItems
,而是反过来。因此,如果我添加项目f,这将起作用rom Honda,但如果我尝试在汽车实例中添加plain Car.CarItems,那是行不通的。我想我需要重新考虑我的设计。
// 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));

        }
    }
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(){ ... }
  }
}