0

我在试图找到解决方案(如果有的话)时遇到了麻烦:

public class Generics {
    Map<Class<? extends SomeObject1>, SomeObject2>> map;
    map = new HashMap<Class<? extends SomeObject1>, SomeObject2>>();

    public static <E extends SomeObject1> SomeObject2 get(Class<E> c) {
        if (map.containsKey(c))
           return map.get(c);
        else {
           SomeObject2 o = new SomeObject2();
           map.put(c, o);
           return o;
        }
    }
}
...
//somewhere
public <T extends SomeObject1> void aMethod(AnInterestedClass<T> list) {
    // How to get the value from the map
    // knowing that the key is of type T?
    Generics.get(); 
}

想法?

4

1 回答 1

1

由于类型擦除,您只能通过将 Class 对象传递给aMethod. 请参阅此相关线程

于 2012-06-21T07:50:36.103 回答