具有多个泛型参数的java lambda表达式未编译
我正在编写Excel导入逻辑:具有多个泛型参数的java lambda表达式未编译,java,generics,lambda,parameters,Java,Generics,Lambda,Parameters,我正在编写Excel导入逻辑: public class ExcelImporter { //...ignore constructor and other methods ... public <R> Builder<R> sheet(String sheetName, RowConsumer<R> rowConsumer) { return new Builder<R>(sheetName, rowConsume
public class ExcelImporter {
//...ignore constructor and other methods ...
public <R> Builder<R> sheet(String sheetName, RowConsumer<R> rowConsumer) {
return new Builder<R>(sheetName, rowConsumer);
}
public class Builder<R> {
//...ignore other method
public <F> Builder header(String name, CellConsumer<R, F> cellConsumer) {
sheetReader.header(new DefaultHeader<>(name, cellConsumer));
return this;
}
public <F> Builder header(String name, Class<F> fieldType, CellConsumer<R, String> cellConsumer) {
return header(name,cellConsumer);
}
}
}
setSex()
无法编译,而且行
计算为对象
,我很困惑,它第一次运行良好,但下次失败
这是cellcuster
:
@FunctionalInterface
public interface CellConsumer<R,F> {
void read(F cell,R row);
}
@FunctionalInterface
public interface RowConsumer<R> {
R newRow();
}
和错误:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.797 s
[INFO] Finished at: 2017-07-28T23:56:26+08:00
[INFO] Final Memory: 17M/166M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:testCompile (default-testCompile) on project graceful-excel: Compilation failure
[ERROR] /home/terrason/workspace/maven/cnx/graceful-excel/src/test/java/cn/lenyar/excel/ExcelImporterTest.java:[81,66] 找不到符号
[ERROR] 符号: 方法 setSex(java.lang.Object)
[ERROR] 位置: 类型为java.lang.Object的变量 row
[ERROR] -> [Help 1]
请帮帮我 问题在于您没有在header方法中指定返回的生成器的泛型类型:
public <F> Builder header(...)
//this returns Builder which is the same as Builder<?>
//All java can infer from Builder<?> is that the generic type is an Object.
//which makes row in the second call an Object
你能粘贴你得到的完全错误和
CellConsumer
界面吗。@hagrawal的问题是更新,对不起,中文日志这需要更多的上下文,你正在调用的setName
和setEx
的行类是什么?什么是RowBean
?
public <F> Builder header(...)
//this returns Builder which is the same as Builder<?>
//All java can infer from Builder<?> is that the generic type is an Object.
//which makes row in the second call an Object
public class ExcelImporter {
//...ignore constructor and other methods ...
public <R> Builder<R> sheet(String sheetName, RowConsumer<R> rowConsumer) {
return new Builder<R>(sheetName, rowConsumer);
}
private static class RowBean {
private String name;
private String sex;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class Builder<R> {
public Builder(String sheetName, RowConsumer<R> rowConsumer) {
}
//...ignore other method
public <F> Builder<R> header(String name, CellConsumer<R, F> cellConsumer) {
return this;
}
public <F> Builder<R> header(String name, Class<F> fieldType, CellConsumer<R, String> cellConsumer) {
return header(name, cellConsumer);
}
public ExcelImporter build() {
return null;
}
}
@FunctionalInterface
public interface CellConsumer<R, F> {
void read(F cell, R row);
}
@FunctionalInterface
public interface RowConsumer<R> {
R newRow();
}
public static void main(String[] args) {
ExcelImporter excelImporter = new ExcelImporter()
.sheet("Sheet1", () -> new RowBean())
.header("姓名", String.class, (cell, row) -> row.setName(cell)) // no error
.header("性别", String.class, (cell, row) -> row.setSex(cell)) // no error!
.build();
}
}