首页 > 解决方案 > 为什么在我的代码中我有这个继承问题?创建一个传递特殊类型而不是声明的抽象超类型的对象

问题描述

在处理 Java 应用程序时,我发现以下与继承相关的问题。

我有以下情况:

1)我定义了一个名为TrendFromExcelAbstract的抽象类。此类包含其他类共有的一些基本字段,这些字段扩展它并表示 Excel 文件的不同选项卡(但这现在不重要):

public abstract class TrendFromExcelAbstract {

    private Long id;
    private String date;
    private String time;
    private String excelDocumentName;
    private String excelDocumentSheet;

    public TrendFromExcelAbstract() {
        super();
    }

    public TrendFromExcelAbstract(Long id, String date, String time, String excelDocumentName,
            String excelDocumentSheet) {
        super();
        this.id = id;
        this.date = date;
        this.time = time;
        this.excelDocumentName = excelDocumentName;
        this.excelDocumentSheet = excelDocumentSheet;
    }
    ................................................................
    ................................................................
    GETTER AND SETTER METHODS
    ................................................................
    ................................................................
}

然后我有一个名为CompVibrAndTempDTO的类,它扩展了前面的抽象类:

public class CompVibrAndTempDTO extends TrendFromExcelAbstract {

    // Temperature reading point
    private String tempReadingPointA;
    private String tempReadingPointB;
    private String tempReadingPointC;
    private String tempReadingPointD;
    private String tempReadingPointE;
    private String tempReadingPointF;
    private String tempReadingPointG;
    private String tempReadingPointH;
    private String tempReadingPointI;
    private String tempReadingPointJ;
    private String tempReadingPointK;
    private String tempReadingPointL;
    private String tempReadingPointM;
    private String tempReadingPointN;
    private String tempReadingPointO;
    private String tempReadingPointP;

    // Vibration reading point
    private String vibrReadingPointA;
    private String vibrReadingPointB;
    private String vibrReadingPointC;
    private String vibrReadingPointD;
    private String vibrReadingPointE;
    private String vibrReadingPointF;
    private String vibrReadingPointG;
    private String vibrReadingPointH;
    private String vibrReadingPointI;
    private String vibrReadingPointJ;
    private String vibrReadingPointK;
    private String vibrReadingPointL;

    public CompVibrAndTempDTO() {
        super();
    }

    public CompVibrAndTempDTO(Long id, String date, String time, String excelDocumentName, String excelDocumentSheet,
            String tempReadingPointA, String tempReadingPointB, String tempReadingPointC, String tempReadingPointD, 
            String tempReadingPointE, String tempReadingPointF, String tempReadingPointG, String tempReadingPointH, 
            String tempReadingPointI, String tempReadingPointJ, String tempReadingPointK, String tempReadingPointL,
            String tempReadingPointM, String tempReadingPointN, String tempReadingPointO, String tempReadingPointP,
            String vibrReadingPointA, String vibrReadingPointB, String vibrReadingPointC, String vibrReadingPointD, 
            String vibrReadingPointE, String vibrReadingPointF, String vibrReadingPointG, String vibrReadingPointH,
            String vibrReadingPointI, String vibrReadingPointJ, String vibrReadingPointK, String vibrReadingPointL) {

         ........................................................................................................................
         ........................................................................................................................
         ........................................................................................................................
    }

    ........................................................................................................................
    ........................................................................................................................
    GETTER AND SETTER METHODS
    ........................................................................................................................
    ........................................................................................................................
}

然后我有另一个名为ExcelTabGeneralInfoDTO的 DTO 类,如下所示:

public class ExcelTabGeneralInfoDTO {

    private String excelDocumentName;
    private String excelSheetName;

    private List<TrendFromExcelAbstract> trendOfTabList;

    public ExcelTabGeneralInfoDTO() {
        super();
    }

    public ExcelTabGeneralInfoDTO(String excelDocumentName, String excelSheetName,
            List<TrendFromExcelAbstract> trendOfTabList) {
        super();
        this.excelDocumentName = excelDocumentName;
        this.excelSheetName = excelSheetName;
        this.trendOfTabList = trendOfTabList;
    }
..................................................................
..................................................................
GETTER AND SETTER METHODS
..................................................................
..................................................................
}

如您所见,此类包含此列表字段:

private List<TrendFromExcelAbstract> trendOfTabList;

这个想法是这个字段表示从TrendFromExcelAbstract派生的任何对象的列表

问题是,在另一堂课中,我正在尝试做这样的事情:

public List<ExcelTabGeneralInfoDTO> getCompVibrAndTempTab() {

    List<CompVibrAndTempDTO> tabTrendList = excelRepo.findCompVibrAndTempTab();
    String excelDocumentName;
    String excelSheetName;

    if(tabTrendList.size() >=0 ) {
        excelDocumentName = tabTrendList.get(0).getExcelDocumentName();
        excelSheetName = tabTrendList.get(0).getExcelDocumentSheet();
    }

    ExcelTabGeneralInfoDTO result = new ExcelTabGeneralInfoDTO(excelDocumentName, excelSheetName, tabTrendList);

    return result;

}

所以基本上在这一行:

ExcelTabGeneralInfoDTO result = new ExcelTabGeneralInfoDTO(excelDocumentName, excelSheetName, tabTrendList);

我正在尝试创建一个新的ExcelTabGeneralInfoDTO ,将具有 List 类型的 tabTrendList 列表传递它。

我收到以下错误消息:

Description Resource    Path    Location    Type
The constructor ExcelTabGeneralInfoDTO(String, String, List<CompVibrAndTempDTO>) is undefined   ExcelServiceImpl.java   /energy-prg-be/src/main/java/com/springboot/excelapi/services   line 425    Java Problem

Description Resource    Path    Location    Type
Type mismatch: cannot convert from List<ExcelTabGeneralInfoDTO> to List<CompVibrAndTempDTO> ExcelResource.java  /energy-prg-be/src/main/java/com/springboot/excelapi/resources  line 167    Java Problem

我的想法是我可以使用更通用的类型(ExcelTabGeneralInfoDTO),然后传递子类型(CompVibrAndTempDTO)。但似乎我的假设是错误的。怎么了?我错过了什么?为什么我不能做这样的事情?我该如何解决?

标签: javaoopinheritance

解决方案


ExcelTabGeneralInfoDTO没有为 定义的构造函数List<CompVibrAndTempDTO>,只有为List<TrendFromExcelAbstract>。尝试声明tabTrendList为 aList<TrendFromExcelAbstract>并查看它是否有效。


推荐阅读