从另一个函数Java 11功能接口调用一个fuction时,非法远期参考

发布于 2025-01-23 04:43:17 字数 1000 浏览 0 评论 0原文

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2025-01-30 04:43:17

非法远期参考,当调用getColumngetcsvrow

引用来自 java语言规范

如果所有以下所有内容都是正确的,则是编译时间错误

  • 声明类别或接口C 在使用类变量之后出现文字


编译器告诉您,您正在尝试在声明函数getColumn

任何变量都应在使用该变量的代码上方声明。您应该交换功能顺序以解决此编译错误。


还有更多的问题:

Illegal forward reference, while calling getColumn from getCsvRow

Quote from the Java language specification:

it is a compile-time error if all of the following are true:

  • The declaration of a class variable in a class or interface C appears textually after a use of the class variable;

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 a Column object could be assigned to a List<Column>.

  • Also, it doesn't make much sense to use a function with a void argument Function<Void, CsvRow> - utilize a Supplier<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文