0

私は次の階層を持っています:

abstract class customType<E extends Number>
class customInteger extends customType<Integer>
class customFloat extends customType<Float> 

customInteger と customFloat の両方を受け入れる LinkedList を宣言する

List<customType<Number>> items = new LinkedList<customType<Number>>();

このアプローチは有効ですか?

4

2 に答える 2

2

customType<Number>とは無関係なので、またはcustomType<Integer>
追加する予定がある場合、これは無効です。 次のことを試すことができます。 IntegerFloatitems

List<CustomType<?>> items = new LinkedList<CustomType<?>>();

これをあなたのアイテムのコレクションとして使用してください。アイテムを追加するには、次のように補助的な方法を使用する必要があります。

public static void addItemToList(List<? super CustomType<?>> l, CustomType<? extends Number> o){  
        l.add(o);  
}  

次に、リストに追加できます。

CustomFloat f = new CustomFloat();  
CustomInteger i = new CustomInteger();  
process(items, i);    
process(items, f);  
于 2013-04-07T17:16:52.333 に答える