4
public class ReportItem<ReportType extends Report>{ }
和类
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
}
的东西是方法add(ReportItem<? extends Report>)
是不安全的方式,我可以提供项目与当前报告追平,但绑另一些和编译器不会抱怨。
是否有可能以类型安全的方式编写方法add
,即我们可以仅传递参数ReportItem<T>
,其中T是当前报表的类型。
ReportType是类型参数 – user3663882
好吧,现在检查。我认为这应该是你想要的。 – EpicPandaForce
Wokrs现在完美。但是报告<报告>看起来有点复杂。你不能给一点解释吗? –
user3663882