我的要求是我有一个接口,其中声明了一个方法,两个类实现了相同的接口,其中一个类的返回类型为Set<object type>
,其他LIST<object type>
...请帮助我纠正代码错误。
以下是示例代码:
报告文件 ---界面
public interface Reportfile
{
public <T> parseReadfile();
}
Masterfile.java ---- class one
public class Masterfile implements Reportfile
{
Set<Masterpojo > mset = new TreeSet<Masterpojo>();
public String[] newline = null;
public Set<Masterpojo> parseReadfile()
{
try{
Masterpojo mo = new Masterpojo();
CSVReader mread = new CSVReader(new FileReader(mfile),(delimiter));
while ((newline = mread.readNext()) != null)
{
mo.setmline(newline);
mset.add(mo);
}
}catch(Exception e)
{
log.error("file not found");
}
return mset;
}
}
Transfile.java ---- class 2
public class Transfile implements Reportfile
{
List<Transpojo> tlist = new ArrayList<Transpojo>();
public String[] newline = null;
public List<Transpojo> parseReadfile()
{
try{
Transpojo to = new Transpojo();
CSVReader tread = new CSVReader(new FileReader(Transfile),(delimiter));
while ((newline = tread.readNext()) != null)
{
to.settline(newline);
tlist.add(to);
}
}catch(Exception e)
{
log.error("Transaction file not found");
}
return tlist;
}
}
Masterpojo
public class Masterpojo
{
public String[] mline = null;
public String[] getmline()
{
return mline;
}
public void setmline(String[] mline)
{
this.mline = mline;
}
}
Transpojo
public class Transpojo
{
public String[] tline = null;
public String[] gettline()
{
return tline;
}
public void settline(String[] tline)
{
this.tline = tline;
}
}
当我在cmd中编译代码时,我的错误就像是belo:
Masterfile.java:6: error: Masterfile is not abstract and does not override abstract method <E>parseReadfi
le() in Reportfile
public class Masterfile implements Reportfile
^
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Masterfile.java:34: error: parseReadfile() in Masterfile cannot implement <E>parseReadfile() in Reportfil
e
public Set<Masterpojo> parseReadfile()
^
return type Set<Masterpojo> is not compatible with void
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Transfile.java:7: error: Transfile is not abstract and does not override abstract method <E>parseReadfile
() in Reportfile
public class Transfile implements Reportfile
^
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Transfile.java:33: error: parseReadfile() in Transfile cannot implement <E>parseReadfile() in Reportfile
public List<Transpojo> parseReadfile()
^
return type List<Transpojo> is not compatible with void
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
4 errors
答案 0 :(得分:2)
public interface Reportfile<T>
{
public T parseReadFile();
}
public class Masterfile implements Reportfile<Set<Masterpojo>>
{
public Set<Masterpojo> parseReadfile()
{
...
}
}
public class Transfile implements Reportfile<List<Transpojo>>
{
public List<Transpojo> parseReadfile()
{
...
}
}
但是,您的代码包含大量重复!查看Transpojo
和Masterpojo
。完全一样! (命名除外)
查看Masterfile
和Transfile
,parseReadFile
都以完全相同的方式实现!除了一个返回Set
而另一个返回List
。所有这些都可以简化为类似的东西(不再需要泛型):
public interface Reportfile
{
public Collection<Pojo> parseReadFile();
}
public final class CsvFile implements Reportfile {
@Override
public Collection<Pojo> parseReadFile() {
Collection<Pojo> result = new ArrayList<>();
String[] newline;
try {
CSVReader reader = new CSVReader(new FileReader(file), delimiter);
while ((newline = reader.readNext()) != null) {
result.add(new Pojo(newline));
}
} catch (Exception e) {
log.error("file not found");
}
return result;
}
}
使用Pojo
:
public final class Pojo {
private final String[] line;
public Pojo(String[] line) {
this.line = line;
}
public String[] getLine() {
return line;
}
}
答案 1 :(得分:1)
你的接口应该声明方法parseReadfile(而不是方法本身)的T是什么,所以如果你这样做,那么T是一个通用的ReportFile类型,你可以在实现它的类中声明它。
对于ReportFile.java
public interface Reportfile<T> { public < T > parseReadfile(); }
对于Masterfile.java
public class Masterfile implements Reportfile<Masterpojo>
对于Transfile.java
public class Transfile implements Reportfile<Transpojo>
您还必须更改方法以返回T not now T now:
public T parseReadfile();