【发布时间】:2018-06-12 11:28:02
【问题描述】:
我在下面编写了一个代码,该代码具有一个通用服务抽象类,该类具有该服务在其所有实现中需要执行的大部分通用功能。所有这些消息都需要经过验证,然后传递给各自的实现类,以便对那个对象做任何他们想做的事情
但这里的问题是 Java 编译器抱怨我下面代码中的参数 NotificationMessage 类型错误。
子类
谁能告诉我为什么以及如何解决这个问题?
我的 AbstractService 类:
public abstract class AbstractService<N extends NotificationMessage> implements ServiceInterface {
@Override
public boolean execute() {
NotificationMessage notification = controller.load(); //this call would return object of sub-type of NotificationMessage
processData(notification); //Compiler complains here that the argument is wrong here
}
protected abstract void processData(N notification);
}
}
具体服务类之一:
public class SummaryService extends AbstractService<SummaryNotification> {
@Override
protected void processData(SummaryNotification notification) {
}
}
数据对象:
public class NotificationMessage {
public String getName() {
return "";
}
}
public class SummaryNotification extends NotificationMessage {
public String getName() {
return "";
}
public String getSummary() {
return "";
}
}
【问题讨论】:
-
您发布的代码永远不会编译。请向我们展示您正在处理的实际代码,或者至少是可以重现问题的最小实际版本
-
您的代码中有拼写错误,例如return 被写成 rerun
-
这是最小的实际代码,可能在将代码放在这里时做了太多更改,但它与我的代码相同,是的,在 processData(notification); 处存在编译器错误; ,就是这里的查询
标签: java generics compiler-errors