我正在創建自己的MVP框架,而且我遇到了仿製藥的麻煩。Java方法中的嵌套泛型?
我的演講是這樣定義的,具有子元素這也是通用主持人的內部類持有引用:
public abstract class Presenter<TView extends View, TKey extends Key>
{
protected final HashMap<String, StageInstances<?, ?>> _stages;
public <TChildView extends View, TChildKey extends Key> void addStage(Class<Presenter<TChildView, TChildKey>> stage, String name)
{
_stages.put(name, new StageInstances<TChildView, TChildKey>(stage));
}
// ...
protected class StageInstances<TChildView extends View, TChildKey extends Key>
{
protected Class<Presenter<TChildView, TChildKey>> _presenter;
protected HashMap<Key, Presenter<TChildView, TChildKey>> _instances;
public StageInstances(Class<Presenter<TChildView, TChildKey>> presenter)
{
_presenter = presenter;
_instances = new HashMap<Key, Presenter<TChildView, TChildKey>>();
}
public Presenter<?, ?> getInstance(Key key)
{
if (!_instances.containsKey(key))
{
try
{
_instances.put(key, _presenter.newInstance());
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
return _instances.get(key);
}
}
}
,我有這個
public class ResultsPresenter extends Presenter<ResultsView, Results>
和
一個具體的實現public class SearchPresenter extends Presenter<SearchView, StringKey>
{
// ...
public void bind()
{
addStage(ResultsPresenter.class, "results");
}
}
where ResultsView,SearchView extend View and Results,St ringKey實施重點
的方法addStage(...)拋出以下編譯時錯誤:
**The method addStage(Class<Presenter<TChildView,TChildKey>>, String) in the type
Presenter<SearchView,StringKey> is not applicable for the arguments
(Class<ResultsPresenter>, String)**
任何幫助,或者更好的做法,將不勝感激
這很完美,謝謝 – davecass 2011-06-16 15:16:36