【发布时间】:2021-12-16 17:50:53
【问题描述】:
我有如下界面:
public interface DataExporter {
MultipartFile export() throws IOException;
}
并且下面的抽象类继承自该接口:
public abstract class AbstractDataExporter<T> implements DataExporter {
@Override
public MultipartFile export() throws IOException {
final Iterable<T> entities = getEntities();
// ...
}
protected abstract Iterable<T> getEntities();
}
然后我实现export和getEntities方法如下图:
public class ProductExporter extends AbstractDataExporter<ProductDTO> {
@Override
protected Iterable<ProductDTO> getEntities() {
//
}
}
一切正常,我从 Controller 调用导出方法,如下所示:
private final ProductExporter productExporter;
public ResponseEntity<Resource> exportProduct() throws IOException {
final MultipartFile multipartFile = productExporter.export();
//
}
我的问题是:当我需要另一个导出器类时,例如CategoryExporter 接受一个参数,例如一个UUID,那么我应该如何正确地重载export 和getEntities 方法呢?我通过添加带参数的新方法来重载,但在这种情况下,所有新方法也都需要添加 ProductExporter,这是不必要的。那么,如何解决这个问题呢?
// ??? what about passing parameter:
private final CategoryExporter categoryExporter ;
public ResponseEntity<Resource> exportCategory(UUID uuid) throws IOException {
final MultipartFile multipartFile = categoryExporter.export(uuid);
//
}
【问题讨论】:
标签: java interface polymorphism overloading abstract-class