Java 9中的JShell中的前向参考是什么?

2023年11月17日10:55:53

JShell是一个命令行工具,它使我们可以输入Java语句(简单语句,复合语句,甚至完整的方法和类),对其进行评估并打印结果。

前向引用是指引用我们在JShell中键入的任何代码中都不存在的方法变量类的命令。在JShell中按顺序输入和评估代码后,这些前向引用已暂时无法解析。JShell支持向前引用方法机构返回类型参数类型变量的类型,以及

在下面的代码片段中,在Jshell中创建了一个方法forwardReference()。在声明变量之前,无法调用此方法。如果我们试图尝试调用此方法,它将引发警告消息:“试图forwardReference()调用在声明了变量notYetDeclared之前无法调用的方法

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> void forwardReference() {
...>       System.out.println(notYetDeclared);
...>    }
| created method forwardReference(), however, it cannot be invoked until variable notYetDeclared is declared

jshell> forwardReference()| attempted to call method forwardReference() which cannot be invoked until variable notYetDeclared is declared

在下面的代码片段中,我们声明了返回字符串的“notYetDeclared ”变量。最后,如果我们在JShell中调用forwardReference(),则它会显示“ variable is defined

jshell> String notYetDeclared = "variable is declared"
notYetDeclared ==> "variable is declared"

jshell> forwardReference()variable is declared

  • 更新时间:2023年11月17日10:55:53 ,共 984 字。