简体   繁体   English

Java方法中的嵌套泛型?

[英]Nested generics in Java methods?

I'm creating my own MVP framework, and I'm running into trouble with the generics. 我正在创建自己的MVP框架,并且遇到了泛型的麻烦。

My presenter is defined like this, with an inner class holding references to child elements which are also generic presenters: 我的演示者是这样定义的,内部类包含对子元素的引用,这些子元素也是通用演示者:

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);
        }
    }
}

and I have a concrete implementations of this 我对此有一个具体的实现

public class ResultsPresenter extends Presenter<ResultsView, Results>

and

public class SearchPresenter extends Presenter<SearchView, StringKey>
{
    // ...

    public void bind()
    {
        addStage(ResultsPresenter.class, "results");
    }
}

where ResultsView, SearchView extend View and Results, StringKey implement Key 其中ResultsView,SearchView扩展了View和Results,StringKey实现了Key

The method addStage(...) throws the following compile-time error: 方法addStage(...)引发以下编译时错误:

**The method addStage(Class<Presenter<TChildView,TChildKey>>, String) in the type 
Presenter<SearchView,StringKey> is not applicable for the arguments 
(Class<ResultsPresenter>, String)**

Any help, or better practices, would be greatly appreciated 任何帮助或更好的做法,将不胜感激

Try to change the method prototype to: 尝试将方法原型更改为:

public <TChildView extends View, TChildKey extends Key> void addStage(Class<? extends Presenter<TChildView, TChildKey>> stage, String name)

pay attention that I changed Class<Presenter<TChildView, TChildKey>> to Class<? extends Presenter<TChildView, TChildKey>> 注意我将Class<Presenter<TChildView, TChildKey>>更改为Class<? extends Presenter<TChildView, TChildKey>> Class<? extends Presenter<TChildView, TChildKey>> . Class<? extends Presenter<TChildView, TChildKey>> This will allow you to pass Class of Presenter's subclass instead of Presenter itself. 这将允许您传递Presenter的子类的类,而不是Presenter本身。

I've not tried it myself, but out of a hunch I'd say that 我没有亲自尝试过,但出于直觉,我会说

addStage(Class<Presenter<TChildView, TChildKey>> stage, String name)

should be 应该

addStage(Class<Presenter<? extends TChildView,? extends TChildKey>> stage, String name)

Try Alex suggestion first. 首先尝试Alex建议。 Keeps the code more readable and makes somewhat more sense than mine. 使代码更具可读性,并且比我的更有意义。 If both fail, combine them. 如果两者均失败,则将它们合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM