简体   繁体   English

泛型“方法不会覆盖其超类中的任何方法”

[英]Generics “method does not override any method from its superclass”

I have created an interface with a generic method, when overriding this method in my class that implements this interface the compiler complains saying "method does not override method from its superclass". 我用通用方法创建了一个接口,当在实现该接口的类中重写此方法时,编译器会抱怨说“方法不会从其超类重写方法”。 This problem occur only when i added a generic param to my abstract method. 仅当我向我的抽象方法添加通用参数时,才会出现此问题。

I created an interface whith one generic method and a Class that implements this interface. 我创建了一个接口,该接口带有一个通用方法和一个实现此接口的Class。

package me.work.rest;
public interface RestControllerFacade<T> {
    <P, R, E extends Throwable> R retrieveCollectionResources(P p) throws E ;
}

@RestController
@RequestMapping("api/v1/Users")
public class UserResource implements RestControllerFacade<User> {
    @GetMapping( produces = MediaType.APPLICATION_JSON_VALUE )
    @Override
    public ResponseEntity<Page<User>> retrieveCollectionResources(Pageable 
    pageable) throws ResourceNotFoundException {
        code...
        return ...;
    }
}

The compiler says "method does not override method from its superclass". 编译器说“方法不会从其超类覆盖方法”。 By the way this code is working for me : 顺便说一下,这段代码为我工作:

package me.work.rest;
public interface RestControllerFacade<T> {
    <R, E extends Throwable> R retrieveCollectionResources() throws E ;
}

@RestController
@RequestMapping("api/v1/Users")
public class UserResource implements RestControllerFacade<User> { 
@GetMapping( produces = MediaType.APPLICATION_JSON_VALUE )
    @Override
    public ResponseEntity<Page<User>> retrieveCollectionResources() throws ResourceNotFoundException {
        code...
        return ...;
    }
}

Only when adding the P param to the method the compiler complains! 只有在将P参数添加到方法时,编译器才会抱怨! Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

If you want that to be a valid override, then P , R , and E need to be generics on the interface , not the method. 如果要使其成为有效的替代,则PRE需要在接口上是泛型,而不是方法上的泛型。 It has to be 它一定要是

public interface RestControllerFacade<P, R, E extends Exception, T> {
    R retrieveCollectionResources(P p) throws E ;
}

@RestController
@RequestMapping("api/v1/Users")
public class UserResource implements 
        RestControllerFacade<
           Pageable, 
           ResponseEntity<Page<User>>, 
           ResourceNotFoundException, 
           User> {
    @GetMapping( produces = MediaType.APPLICATION_JSON_VALUE )
    @Override
    public ResponseEntity<Page<User>> retrieveCollectionResources(Pageable 
    pageable) throws ResourceNotFoundException {
        code...
        return ...;
    }
}

The way you have written it, any RestControllerFacade has to be able to work for any P , R , and E , not just some particular combination. 按照您的编写方式, 任何 RestControllerFacade必须能够适用于任何 PRE ,而不仅仅是某些特定的组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM