2014-05-19 44 views
0

我有一個類,其中包含一個我重寫的內部類。這似乎工作正常。Casting List <>覆蓋Java中的內部類

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

但願這不是太多的代碼可循的,這一切都非常直截了當...我得到的錯誤是#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 IS-A Car.CarItems,爲什麼不會是讓我一個列表添加到列表?

+0

「這一切都非常簡單」......並非如此。 –

+1

在你的代碼中,你正試圖將'List '添加到'List '中。你不能這樣做,因爲'Car.CarItems'不一定是'Honda.CarItems',最具體的說,'Car.CarItems'不會擴展'Honda.CarItems',反之亦然。 – LJ2

回答

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.CarI(顯式或隱式地) tems to 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(){ ... } 
    } 
} 
1

嘗試讓經銷商CarItems集合使用泛型擴展功能。然後,在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() { 
     } 
    } 
} 
+0

所以,如果我從本田添加項目,但如果我嘗試將Car.CarItems添加到Car的一個實例,那就行不通了。我想我需要重新考慮我的設計。 –

相關問題