作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 POJO 指定为:MyClass<U>
, 其中U
是泛型类型参数。我正在尝试编写一个接受类引用的实用方法 Class<T>
并填充 Map<String, T>
类型的 map (接受 map 填充)。
这个方法是这样实现的:
static void populateMap(Map<String, T> map, Class<T> type) {
...
// Parses into the specified type and returns an object of that type.
T obj = parse(..., type);
map.put (key, obj);
...
return map;
}
这编译得很好。在我的来电者中,我尝试用任何 MyClass
填充 map 实例(不考虑类型)作为值。因此我使用以下代码:
// Loses type information
Map<String, MyClass<?>> m = new HashMap<>();
populateMap(m, MyClass.class);
这不会编译。编译错误:
The method
populate(Map<String,T>, Class<T>)
in the type ... is not applicable for the arguments(Map<String,MyClass<?>>, Class<MyClass>)
我该如何解决这个问题?
最佳答案
在这种情况下,对 Class<MyClass<?>>
进行未经检查的转换应该是安全的:
// This is okay because we're switching to a type with an unbounded wildcard -
// the behaviors of Class.newInstance and Class.cast are still safe.
@SuppressWarnings("unchecked")
Class<MyClass<?>> classWithNarrowedType =
(Class<MyClass<?>>)(Class<?>)MyClass.class;
populateMap(m, classWithNarrowedType);
这是一个笨拙的解决方案,特别是如果您有许多这样的调用站点,但无法回避这样一个事实,即类文字使用原始类型进行参数化,使其用作参数化类型的工厂,如 MyClass<T>
天生尴尬。
一个可能更清洁的解决方案将解耦 populateMap
来自类文字的使用:
interface Parser<T> {
T parse();
}
static void populateMap(Map<String, T> map, Parser<T> parser) { ... }
...
Map<String, MyClass<?>> m = new HashMap<>();
Parser<MyClass<?>> myClassParser = new Parser<MyClass<?>>() {
@Override
public MyClass<?> parse() {
return parse(..., MyClass.class);
}
};
populateMap(m, myClassParser);
顺便说一句,我建议使用更灵活的签名(有关更多信息,请参阅 What is PECS (Producer Extends Consumer Super)?):
static void populateMap(Map<String, ? super T> map, Parser<T> parser)
关于Java 泛型 : How to specify a Class type for a generic typed class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940376/
我是一名优秀的程序员,十分优秀!