Java中的泛型特定接口定义

Boj*_*vic 2 java generics

是否可以在Java中定义以下内容:

public interface IGenericRepo<T> {
    void add();
    void delete();
    void attach();
}

public interface IGenericRepo<Book> {
    default String bookSpecificMethod(){
      return "smthn";
    }
}

public class NHGenericRepo<T> implements IGenericRepo<T>{
    /* implementation */
}

public class NHUnitOfWork implements UnitOfWork{
    @Autowired
    public void setBookRepo(NHGenericRepo<Book> bookRepo) {
        this.bookRepo= bookRepo;
    }
    public NHGenericRepo<Book> getBookRepo() {
       return bookRepo;
    }
    private NHGenericRepo<Book> bookRepo;
}
Run Code Online (Sandbox Code Playgroud)

并且能够在代码中的某个地方拥有:

{
    @Autowired
    public void setNhuw(NHUnitOfWork nhuw) {
        this.nhuw = nhuw;
    }

    private NHUnitOfWork nhuw;

    /**/

    {
        String st = this.nhuw.getBookRepo().bookSpecificMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

在.net中,可以使用扩展方法将"this IGenericRepo <Book>"作为第一个方法参数.

Tim*_*m B 5

你最接近的是:

public interface IBookGenericRepo extends IGenericRepo<Book> {
    void BookSpecificMethod();
}
Run Code Online (Sandbox Code Playgroud)