Ho battuto la testa contro questo per un po 'e ho pensato che forse alcuni occhi nuovi vedranno il problema; Grazie per il tuo tempo.Java Generics Puzzler, estensione di una classe e utilizzo di caratteri jolly
import java.util.*;
class Tbin<T> extends ArrayList<T> {}
class TbinList<T> extends ArrayList<Tbin<T>> {}
class Base {}
class Derived extends Base {}
public class Test {
public static void main(String[] args) {
ArrayList<Tbin<? extends Base>> test = new ArrayList<>();
test.add(new Tbin<Derived>());
TbinList<? extends Base> test2 = new TbinList<>();
test2.add(new Tbin<Derived>());
}
}
Utilizzo di Java 8. Sembra a me come la creazione diretta del contenitore in test
è equivalente al contenitore in test2
, ma il compilatore dice:
Test.java:15: error: no suitable method found for add(Tbin<Derived>)
test2.add(new Tbin<Derived>());
^
Come faccio a scrivere Tbin
e TbinList
quindi l'ultima riga è accettabile?
Nota che in realtà sto aggiungendo il dattiloscritto Tbin
s che è il motivo per cui ho specificato Tbin<Derived>
nell'ultima riga.
In Eclipse il messaggio di errore è 'Il metodo add (Tbin) nel tipo ArrayList > non è applicabile per gli argomenti (Tbin ) '. –
dimo414
Prova questo 'ArrayList> test3 = new TbinList <>(); '. Davvero molto interessante. –
Radiodef
@Radiodef Eclipse mostra un errore dicendo che "non può dedurre argomenti di tipo per TbinList <>". – TNT