标题翻译
Why in my code do I have this inheritance problem? Create an object passing a specialized type instead of declared abstract super type
问题
在一个Java应用程序中,我遇到了与继承相关的问题。
我有以下情况:
- 我定义了一个名为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 {
// 温度读数点
private String tempReadingPointA;
private String tempReadingPointB;
// ... 更多字段 ...
private String vibrReadingPointL;
public CompVibrAndTempDTO() {
super();
}
// ... 构造方法 ...
// ... 更多代码 ...
}
然后我有另一个名为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
我得到以下错误消息:
The constructor ExcelTabGeneralInfoDTO(String, String, List<CompVibrAndTempDTO>) is undefined
我的想法是我可以使用更通用的类型(ExcelTabGeneralInfoDTO),然后传递子类型(CompVibrAndTempDTO)。但似乎我的假设是错误的。出了什么问题?我漏掉了什么?为什么我不能像这样做?我该如何修复它?
英文翻译
Working on a Java application I am finding the following problem related to inheritance.
I have the following situation:
-
I defined an abstract class named TrendFromExcelAbstract. This class contains some basic fields common to other classes which extend it and represent different tabs of an Excel file (but this is not important now):
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 ................................................................ ................................................................
}
Then I have a class named CompVibrAndTempDTO extending the previous abstract class:
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
........................................................................................................................
........................................................................................................................
}
Then I have another DTO class named ExcelTabGeneralInfoDTO like this:
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
..................................................................
..................................................................
}
As you can see this class contains this list field:
private List<TrendFromExcelAbstract> trendOfTabList;
The idea is that this field represents a list of any objects that derive from TrendFromExcelAbstract
The problem is that in another class I am trying to do something like this:
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;
}
So basically at this line:
ExcelTabGeneralInfoDTO result = new ExcelTabGeneralInfoDTO(excelDocumentName, excelSheetName, tabTrendList);
I am trying to create a new ExcelTabGeneralInfoDTO, passing to it the tabTrendList list having type List<CompVibrAndTempDTO>.
I get the following error message:
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
My idea is that I can use the more general type (ExcelTabGeneralInfoDTO) and then pass the child type (CompVibrAndTempDTO). But it seems that my assumption is false. What is wrong? What am I missing? Why can't I do something like this? How can I fix it?
答案1
得分: -1
ExcelTabGeneralInfoDTO
没有为List<CompVibrAndTempDTO>
定义的构造函数,只有为List<TrendFromExcelAbstract>
定义的构造函数。尝试将tabTrendList
声明为List<TrendFromExcelAbstract>
,然后看看是否有效。
英文翻译
ExcelTabGeneralInfoDTO
does not have a defined constructor for List<CompVibrAndTempDTO>
, only for List<TrendFromExcelAbstract>
. Try declaring tabTrendList
as a List<TrendFromExcelAbstract>
and see if it works.
专注分享java语言的经验与见解,让所有开发者获益!
评论