类对象的通用类型规范

jav*_*vaj 4 java generics

我正在使用HashMap和下面显示的方法来跟踪类类型的更改侦听器.IDE正在发出警告

[rawtypes] found raw type: java.lang.Class
  missing type parameters for generic class java.lang.Class<T>. 
Run Code Online (Sandbox Code Playgroud)

需要指定什么类型的类来解决警告?

private HashMap<Class, Set<ChangeListener>> classChangeListeners;

/**
 * Adds a ChangeListener to the listener list for the specified class type. The class type
 * specified must be a subclass of {@link BusinessObject}.
 *
 * @param  <T>  the type of BusinessObject.
 * @param  cls  the class type.
 * @param  listener  the ChangeListener to be added.
 */
public <T extends BusinessObject> void addChangeListener(Class<T> cls, ChangeListener listener)
{
 if (!classChangeListeners.containsKey(cls))
 {
  classChangeListeners.put(cls, new TreeSet<ChangeListener>());
 }

 classChangeListeners.get(cls).add(listener);
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ker 5

如果您不关心指定Class您正在使用的特定es 的类型,则可以使用Class<?>而不是原始类型 Class.

在某些情况下,你想要通配符,Class<? extends BusinessObject>或参数化它,Class<T>- 但通常Class<?>就足够了,而且很难从短片段推断出你真正想要的东西.

此外,您似乎正在使用包含集合的Map.您可以考虑使用内置此功能的Multimap,并使其更好用.