独自のMVPフレームワークを作成していますが、ジェネリックに問題が発生しています。
私のプレゼンターはこのように定義されており、一般的なプレゼンターでもある子要素への参照を保持する内部クラスがあります。
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");
}
}
ここで、ResultsView、SearchViewはViewとResultsを拡張し、StringKeyはKeyを実装します
メソッドaddStage(...)は、次のコンパイル時エラーをスローします。
**The method addStage(Class<Presenter<TChildView,TChildKey>>, String) in the type
Presenter<SearchView,StringKey> is not applicable for the arguments
(Class<ResultsPresenter>, String)**
任意の助け、またはより良い実践は、大いに感謝されます