1

これらのクラスとインターフェースを持つ..

public interface Shape;

public interface Line extends Shape

public interface ShapeCollection< Shape>

public class MyClass implements ShapeCollection< Line>

List< ShapeCollection< Shape>> shapeCollections = new LinkedList< ShapeCollection< Shape>>();

のインスタンスを に追加しようとすると、Eclipse は への拡張機能を実装しているため、既に実装されている場合でもMyClass実装を許可するよう求めます。に変更しようとしましたが、結果はありません。どんな助けでも大歓迎です。shapeCollectionsMyClassShapeCollection< Shape>ShapeCollection< Line>LineShapeShapeCollection< T extends Shape>

4

2 に答える 2

2

名前Shapeなどの型パラメータを宣言しました。境界Lineを宣言していません。つまり、次の 2 つの宣言は同じです。

public interface ShapeCollection<Shape> // generic parameter called Shape
public interface ShapeCollection<T>  // generic parameter called T

しかし、あなたが望むのは:

public interface ShapeCollection<T extends Shape> // generic parameter bound to Shape

それを使用する場合、あなたの質問を文字通り読んだ場合、あなたは aMyClassに aを追加しようとしていますList<ShapeCollection<Shape>>MyClass、のコレクションではなく、extendsShapeのコレクションでLineあり、 ではなくタイプとして使用する必要があります。LineShape? extends ShapeShape

List<ShapeCollection<? extends Shape>> shapeCollections = new LinkedList<ShapeCollection<? extends Shape>>();
shapeCollections.add(new MyClass()); // should work

これは、がのサブクラスCollection<Line>はないためです。ジェネリックはクラス階層とは異なります。Collection<Shape>

于 2013-10-19T13:50:16.690 に答える