java - From Class<? extends X> to X -
i working on reflexion stuff , i'm beginner @ it.
i got :
list<x> list; set<class<? extends x>> set;
and want :
for (class<? extends x> classx : set) list.add(classx);
but couldn't find on how achieve it. can me?
you can't achieve such behavior input data. list<x>
list expected instance of class x, you're trying add object of type class<? extends x>
. let's imagine type x number(for simplicity), have double, integer, character , etc, children of number. so, you're doing next thing: want add object of type class<integer>
list can hold numbers(not class<integer>
, class<double>
, on). thing can do, it's create instance of class<integer>
, class<double>
way, using reflection:
list<x> list = //some values; set<class<? extends x>> set = //some values; (class<? extends x> classx : set) list.add(classx.newinstance());
but that's bad idea use reflection in such way. maybe meant this:
list<class<x>> list = //some values; set<class<? extends x>> set = //some values; (class<? extends x> classx : set) list.add(classx);
because still class<x>
not same class<? extends x>
. maybe meant this:
list<x> list = //some values; set<? extends x> set = = //some values; (x elem : set) list.add(elem);
last 1 looks nice , should work.
Comments
Post a Comment