i have class
public class reportitem<reporttype extends report>{ } and class
public abstract class report implements iterable<reportitem>{ private list<reportitem<? extends report> > itemlist; public void add(reportitem<? extends report> item){ itemlist.add(item); } //some other staff } public class concretereport extends report{ //some staff } the thing method add(reportitem<? extends report>) unsafe in way provide items aren't tied current report, tied , compiler won't complain.
is possible write method add in type-safe way, i.e. pass argument reportitem<t> t type of current report.
i think looking following.
public abstract class report<t extends report<t>> implements iterable<reportitem<t>>{ private list<reportitem<t>> itemlist; public void add(reportitem<t> item){ itemlist.add(item); } //some other stuff } public class concretereport extends report<concretereport> { //some stuff } the way works that:
- you want parametrize
reportitem<t>extendsreport - you want make sure list of
reportitem<t>belong same type ofreport
in order bind t parameter of reportitem<t> extends report, need parametrize report itself:
public abstract class report<t> implements iterable<reportitem<t>> you add bind needs extend report
public abstract class report<t extends report> implements iterable<reportitem<t>> but specifying bound raw type of report, doesn't work, need provide report type parameter report receives, t.
public abstract class report<t extends report<t>> implements iterable<reportitem<t>> this way can parametrize list<reportitem<t>> concrete type extend with:
public class concretereport extends report<concretereport> { this way list be
public list<reportitem<concretereport>> itemlist; which wanted.
and works! :) hope explanation of makes sense.
Comments
Post a Comment