使用通用时出现问题
Issue while using generic
我有以下 Genric class 用于设置任何自定义数据类型
public class NotificationData<E> {
private E element;
public E getElement() {
return element;
}
下方显示编译时错误的通知任务界面 -
T cannot be resolved to a type. I add "" in front of the method it does resolve error but creating issue for other classes which are using this interface.
public interface NotificationTask {
void execute(NotificationData<T> taskData);
// --other methods
}
下面class实现了它但是显示错误
Name clash: The method execute(NotificationData) of type
AbstractEmailNotificationTask has the same erasure as
execute(NotificationData) of type NotificationTask but does not
override it
和 prepareEmailTemplate 显示以下错误-
The method prepareEmailTemplate(NotificationData) from the type
AbstractEmailNotificationTask refers to the missing type T
public abstract class AbstractEmailNotificationTask implements NotificationTask{
private static final Log logger = LogFactory.getLog(AbstractEmailNotificationTask.class);
private boolean flag;
public <T> void execute(NotificationData<?> taskData) {
try {
String content=prepareEmailTemplate(taskData);
setTaskExceuted(true);
} catch (Exception e) {
logger.debug (e.getMessage (),e);
setTaskExceuted(false);
}
}
abstract protected String prepareEmailTemplate(NotificationData<T> taskData) throws TaskExecutionException;
}
public class AddressUpdateEmailNotification extends AbstractEmailNotificationTask {
public AddressUpdateEmailNotification() {
}
@Override
protected String prepareEmailTemplate(NotificationData<CustomerAddress> taskData) {
CustomerAddress customerAddress= taskData.getElement();
return customerAddress.getCity() +":"+customerAddress.getState().getStateName();
}
}
以上是将实施 prepareEmail 模板的实际 class。在此 class 中,我传递了 CustomerAddress,但在扩展 AbstractEmailNotificationTask 的其他 类 中,我必须传递其他对象。我想使用通用但面临这些问题。
您的接口不是泛型的,因此其中的方法无法被泛型类型识别(并且无法解析为一个)。简单地使其通用:
public interface NotificationTask<T> {
void execute(NotificationData<T> taskData);
// --other methods
}
我有以下 Genric class 用于设置任何自定义数据类型
public class NotificationData<E> {
private E element;
public E getElement() {
return element;
}
下方显示编译时错误的通知任务界面 -
T cannot be resolved to a type. I add "" in front of the method it does resolve error but creating issue for other classes which are using this interface.
public interface NotificationTask {
void execute(NotificationData<T> taskData);
// --other methods
}
下面class实现了它但是显示错误
Name clash: The method execute(NotificationData) of type AbstractEmailNotificationTask has the same erasure as execute(NotificationData) of type NotificationTask but does not override it
和 prepareEmailTemplate 显示以下错误-
The method prepareEmailTemplate(NotificationData) from the type AbstractEmailNotificationTask refers to the missing type T
public abstract class AbstractEmailNotificationTask implements NotificationTask{
private static final Log logger = LogFactory.getLog(AbstractEmailNotificationTask.class);
private boolean flag;
public <T> void execute(NotificationData<?> taskData) {
try {
String content=prepareEmailTemplate(taskData);
setTaskExceuted(true);
} catch (Exception e) {
logger.debug (e.getMessage (),e);
setTaskExceuted(false);
}
}
abstract protected String prepareEmailTemplate(NotificationData<T> taskData) throws TaskExecutionException;
}
public class AddressUpdateEmailNotification extends AbstractEmailNotificationTask {
public AddressUpdateEmailNotification() {
}
@Override
protected String prepareEmailTemplate(NotificationData<CustomerAddress> taskData) {
CustomerAddress customerAddress= taskData.getElement();
return customerAddress.getCity() +":"+customerAddress.getState().getStateName();
}
}
以上是将实施 prepareEmail 模板的实际 class。在此 class 中,我传递了 CustomerAddress,但在扩展 AbstractEmailNotificationTask 的其他 类 中,我必须传递其他对象。我想使用通用但面临这些问题。
您的接口不是泛型的,因此其中的方法无法被泛型类型识别(并且无法解析为一个)。简单地使其通用:
public interface NotificationTask<T> {
void execute(NotificationData<T> taskData);
// --other methods
}