从另一个函数Java 11功能接口调用一个fuction时,非法远期参考
@Getter @Setter
Class Row{
private int position;
private String name;
private List<Column> columns;
}
@Getter @Setter
Class Column{
private int position;
private String name;
private String description;
}
@UtilityClass
class CsvUtils{
public Function<Void, Row> getCsvRow = (nothing) -> {
Row row = new Row();
row.setPosition(1);
row.setName("Student Details");
// CE : Illegal forward reference
List<Column> columns = getCsvColumn.apply(null);
row.setColumns(columns);
return row;
};
public Function<Void, Column> getColumn = (nothing) -> {
Column csvColumn = new Column();
csvColumn.setPosition(1);
csvColumn.setName("Marks");
return csvColumn;
};
}
从GetCsvrow致电GetColumn的同时,获得非法远期参考。 列表列= getcsvcolumn.apply(null);
需要您的帮助来解决这个问题。
我知道的一种方法是将功能接口转换为Java方法,然后我可以从“ getCsvrow”中调用“ getColumn”,
以寻找其他解决方案
@Getter @Setter
Class Row{
private int position;
private String name;
private List<Column> columns;
}
@Getter @Setter
Class Column{
private int position;
private String name;
private String description;
}
@UtilityClass
class CsvUtils{
public Function<Void, Row> getCsvRow = (nothing) -> {
Row row = new Row();
row.setPosition(1);
row.setName("Student Details");
// CE : Illegal forward reference
List<Column> columns = getCsvColumn.apply(null);
row.setColumns(columns);
return row;
};
public Function<Void, Column> getColumn = (nothing) -> {
Column csvColumn = new Column();
csvColumn.setPosition(1);
csvColumn.setName("Marks");
return csvColumn;
};
}
Getting CE that is Illegal forward reference, while calling getColumn from getCsvRow.
List columns = getCsvColumn.apply(null);
Need your help to solve this problem.
One way i know that is convert functional interface to java methods then i can call method "getColumn" from "getCsvRow"
Looking for other solutions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用来自 java语言规范:
编译器告诉您,您正在尝试在声明函数
getColumn
。任何变量都应在使用该变量的代码上方声明。您应该交换功能顺序以解决此编译错误。
还有更多的问题:
除了
csv
前缀以随机出现在名称中的杂物原因外,尚不清楚结果如何返回产生column
的函数。对象可以分配给list&lt; lt;
。另外,使用void参数
函数&lt; void,csvrow&gt;
- 利用供应商。因为您不是在转换函数责任的数据,而是需要提供一个对象,这就是供应商接口的目的。
Quote from the Java language specification:
The compiler is telling you that you are trying to use function
getColumn
before it has been declared.Any variable should be declared above the code that uses that variable. You should swap the order of functions to fix this compilation error.
There are few more issues:
Apart from the mess causes by
Csv
prefix that appears randomly in the names, it's unclear how the result return the function that produces aColumn
object could be assigned to aList<Column>
.Also, it doesn't make much sense to use a function with a void argument
Function<Void, CsvRow>
- utilize aSupplier<CsvRow>
instead. Because you are not transforming the data which is the responsibility of function instead, but you need to provide an object and that is what supplier interface is meant for.