如何正确覆盖Java中的函数?

时间:2017-12-31 20:15:53

标签: java

我有以下BO的

@Data
public abstract class CommonBO extends BaseBO{

    @NotBlank(message ="{validation.not.blank}")
    @Length(max = 100)
    String name;
}


@Data
@MappedSuperclass
public abstract class CommonPersist extends BasePersist {

    @Column(unique = true,nullable = false)
    private String name;

}

然后我的BaseController

public abstract class BaseController <T extends BasePersist, K extends BaseBO> {

 @RequestMapping(value = "/validate", method = RequestMethod.POST)
    @ResponseBody
    public ValidationResponse validate(@Valid K t,
                                       BindingResult result, Locale locale) {
        ValidationResponse res = new ValidationResponse();
        List<ErrorMessage> errorMessages = new ArrayList<>();
        if (!result.hasErrors()) {
            res.setStatus(MessageTypes.SUCCESS);
        } else {
            res.setStatus(MessageTypes.FAIL);
            List<FieldError> allErrors = result.getFieldErrors();
            errorMessages = new ArrayList<>();
            for (FieldError objectError : allErrors) {
                errorMessages.add(new ErrorMessage(objectError.getField(),
                        objectError.getDefaultMessage()));
            }
        }
        res.setErrorMessageList(errorMessages);
        return res;
    }

}

最后,我的另一个抽象控制器

public abstract class CommonController<T extends CommonPersist, K extends CommonBO> extends BaseController {

  @Override
    public ValidationResponse validate(@Valid K t, BindingResult result, Locale locale) {
        ValidationResponse res = super.validate(t, result, locale);
        if (res.getStatus().equalsIgnoreCase(MessageTypes.SUCCESS)) {
            if (commonService.checkIfNameIsUsed(t.getName(), t.getId())) {
                ErrorMessage em = new ErrorMessage("name", messageSource.getMessage("error.duplicate.is.used", null, locale));
                res.getErrorMessageList().add(em);
                res.setStatus(MessageTypes.FAIL);
            }
        }
        return res;
    }
}

@Override注释给出错误“Method不会覆盖其超类中的方法”。我想从CommonBo extends BaseBO开始,@ Override函数应该覆盖BaseController中的方法?

更新

基于@Slaks的回答

public abstract class CommonController<T extends CommonPersist, K extends CommonBO> extends BaseController<T,K> {

@Override
    public ValidationResponse validate(@Valid K t, BindingResult result, Locale locale) {
        ValidationResponse res = super.validate(t, result, locale);
        if (res.getStatus().equalsIgnoreCase(MessageTypes.SUCCESS)) {
            if (commonService.checkIfNameIsUsed(t.getName(), t.getId())) {
                ErrorMessage em = new ErrorMessage("name", messageSource.getMessage("error.duplicate.is.used", null, locale));
                res.getErrorMessageList().add(em);
                res.setStatus(MessageTypes.FAIL);
            }
        }
        return res;
    }
}

1 个答案:

答案 0 :(得分:3)

您的CommonController类扩展了BaseController,但没有指定任何类型参数(<...>)。

因此,编译器会将其解释为BaseController<Object, Object>

您的方法不会将Object作为参数,因此它与基本类型不匹配。

您需要在extends子句中指定类型参数:

public abstract class
  CommonController<T extends CommonPersist, K extends CommonBO>
       extends BaseController<T,K> {