我有一个接收器类,将消息放入消息队列中,如下所示
public class Receiver<T extends MessageBase> {
private LinkedBlockingQueue<T> messageQueue = new LinkedBlockingQueue<T>();
public void add(T msg) {
messageQueue.add(msg);
}
public void Class<T> getType() {
...
}
}
当尝试添加消息时
public <T extends MessageBase> void processMessage(T msg) {
for (Receiver<? extends MessageBase> r : receivers) {
if (msg.getClass().equals(r.getType())) {
r.add(msg);
}
}
}
我得到一个
The method add(capture#3-of ? extends MessageBase) in the type
Receiver<capture#3-of ? extends MessageBase> is not applicable
for the arguments (T)
由于Receiver
该类被强制转换为扩展MessageBase
,因此添加作为子类的消息是否应该不起作用MessageBase
?