简体   繁体   English

java 抽象 class 用于 inheritance

[英]java abstract class for inheritance

I have a project where I am currently loading the records from the excel file to the database using from org.apache.poi我有一个项目,我目前正在使用 org.apache.poi 将 excel 文件中的记录加载到数据库中

I have 3 kinds of files that I am loading into different Dto classes.我有 3 种文件要加载到不同的 Dto 类中。 Two of these dtos shares the same base class (they have common attributes)其中两个 dto 共享相同的基础 class(它们具有共同的属性)

@Getter
@Setter
@ToString
public class PpyRecordDTO {

private String units;
private Double quantity;

}

@Getter
@Setter
@ToString
public class FirstRecordDTO extends RecordDTO {
private String normDescription;
private String relatedUnits;

}


@Getter
@Setter
@ToString
public class SecondRecordDTO extends RecordDTO{

private String normName;
}

@Getter
@Setter
@ToString
public class ThirdRecordDTO {

private String code;
}

The ThirdRecordDto class has a unique attribute and no shared attributes with the base dto class RecordDTO ThirdRecordDto class 具有唯一属性,没有与基础 dto class RecordDTO 共享的属性

I want to return from this method the base class: RecordDto (But ThirdRecordDTO cannot extend it since there are no common fields)我想从此方法返回基础 class: RecordDto (但 ThirdRecordDTO 无法扩展它,因为没有公共字段)

    public static List<?extends RecordDTO> readPpyExcelFile(MultipartFile file, SourceType sourceType){
    //TODO: making readPpyExcelFile generic
    try {
        Workbook workbook = WorkbookFactory.create(new BufferedInputStream(file.getInputStream()));

        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rows = sheet.iterator();

        List<? extends RecordDTO> lstRecords = processRecords(rows, sourceType);

        // Close WorkBook
        workbook.close();

        return lstRecords;
    } catch(ApiGenericException apiException){
        throw new ApiGenericException(apiException.getStatus(), apiException.getMessage(), apiException.getErrorType());
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new ApiGenericException(HttpStatus.INTERNAL_SERVER_ERROR,"Enable while loading the file", ApiErrorType.Unhandled
        );
    }
}

Is there a way to make the dto ThirdRecordDto also be returned or inherit from an abstract class which is shared by the other dtos in order to return the type <?有没有办法使 dto ThirdRecordDto 也被返回或从抽象 class 继承,该抽象 class 由其他 dtos 共享以返回类型 <? extends List from this method?从此方法扩展列表?

In general you can go with something like this:一般来说,你可以用这样的东西 go :

public interface Ppy {
 // common methods if any
}

public class PpyRecordDTO implements Ppy{...}
public class FirstRecordDTO extends PpyRecordDTO {...} // so that it also implements Ppy interface
public class SecondRecordDTO extends PpyRecordDTO {...} // the same as above
public class ThirdRecordDTO implements Ppy {...} // Note, it doesn't extend PpyRecordDTO but implements the interface

Now in the method, it's possible to:现在在该方法中,可以:

 public static List<Ppy> readPpyExcelFile(MultipartFile file, SourceType sourceType){...}

This will work, however, you should ask yourself the following: what will do the code that will call this method, namely how it will differentiate between the different implementations?这将起作用,但是,您应该问自己以下问题:调用此方法的代码将做什么,即它将如何区分不同的实现? If the interface has a common method that makes sense for all implementations - fine, it will be able to call the method.如果接口有一个对所有实现都有意义的通用方法——很好,它将能够调用该方法。 For example, if it has a method like render(Page) or something, the code might be:例如,如果它有类似render(Page)之类的方法,代码可能是:

List<Ppy> ppis = readPpyExcelFile(...);
Page page = ...
for(Ppy ppi : ppis) {
    ppi.render(page);
} 

However, if the interface doesn't have any common methods - it won't help much.但是,如果接口没有任何常用方法 - 它不会有太大帮助。 Inheritance is used when the child object can be viewed as a specialization of Parent (so that the child "is a" parent). Inheritance 在子 object 可以被视为 Parent 的特化时使用(因此子“是”父)。 So think whether inheritance is really appropriate here, assuming ThirdRecordDTO doesn't have anything in common with the rest of the classes.所以想想 inheritance 在这里是否真的合适,假设ThirdRecordDTO与类的 rest 没有任何共同点。

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

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