Genel bir sınıf Foo<T>
ve Foo<String>
ve Foo<Integer>
türetilmiş parametreleri var. Şimdi farklı parametreli türleri tek bir ArrayList
içine koymak istiyorum. Bunu yapmanın doğru yolu nedir?java'da Genel Türleri İçeren
Aday 1:
public class MMM {
public static void main(String[] args) {
Foo<String> fooString = new Foo<String>();
Foo<Integer> fooInteger = new Foo<Integer>();
ArrayList<Foo<?> > list = new ArrayList<Foo<?> >();
list.add(fooString);
list.add(fooInteger);
for (Foo<?> foo : list) {
// Do something on foo.
}
}
}
class Foo<T> {}
aday 2: Bir kelime
public class MMM {
public static void main(String[] args) {
Foo<String> fooString = new Foo<String>();
Foo<Integer> fooInteger = new Foo<Integer>();
ArrayList<Foo> list = new ArrayList<Foo>();
list.add(fooString);
list.add(fooInteger);
for (Foo foo : list) {
// Do something on foo.
}
}
}
class Foo<T> {}
, bu Foo<?>
ve ham türü Foo
arasındaki fark ile ilgilidir.
Güncelleme:
Grep sınırsız joker parametreli tip ve ham tip arasındaki fark nedir? this link numaralı telefondan yararlı olabilir.