Java 9 中 JShell 中有哪些不同的"/types"命令?

javaobject oriented programmingprogramming更新于 2025/10/7 17:16:17

Java 9 版本引入了 JShell 工具。它也被称为 REPL(读取-评估-打印-循环)工具,允许我们执行 Java 代码并立即获得结果。我们需要使用"/types"命令列出声明的类型,如 class、interface、enum 等。

以下是 JShell 中不同的"/types"命令。

/types
/types [ID]
/types [Type_Name]
/types -start
/types -all
  • /types:此命令列出 JShell 中创建的所有活动类型(类、接口、枚举)。
  • /types [ID]:此命令显示与 id [ID] 对应的类型。
  • /types [Type_Name]:此命令显示与 [Type_Name] 对应的类型。
  • /types -start:此命令允许我们列出已添加到 JShell 启动脚本的类型
  • /types -all:此命令允许我们列出当前会话的所有类型(活动、非活动和 JShell 启动时加载的类型)。

在下面的代码片段中,创建了类、接口和枚举类型。然后,我们可以应用不同的"/types"命令。

jshell> enum Operation {
...>       ADDITION,
...>       DIVISION;
...>    }
| created enum Operation

jshell> class Employee {
...>       String empName;
...>       int age;
...>       public void empData() {
...>          System.out.println("Employee Name is: " + empName);
...>          System.out.println("Employee Age is: " + age);
...>       }
...>    }
| created class Employee

jshell> interface TestInterface {
...>       public void sum();
...>    }
| created interface TestInterface
jshell> /types
|    enum Operation
|    class Employee
|    interface TestInterface

jshell> /types 1
|    enum Operation

jshell> /types -start

jshell> /drop Operation
|    dropped enum Operation

jshell> /types -all
|    enum Operation
|    class Employee
|    interface TestInterface

相关文章


有用资源