Java 9引入了一个交互式REPL(Read-Evaluate-Print-Loop)工具:JShell,它允许我们执行代码片段并立即获得结果。一个snippet是一个可以使用标准Java语法的指令。它表示一个单独的表达式、语句或声明。
在使用JShell工具时,我们需要遵循以下一些规则。
在下面的示例代码片段中,我们创建了一个Employee类,其中包含必要的getter方法,并使用new运算符进行实例化。
<strong>jshell> class Employee { ...> private String firstName; ...> private String lastName; ...> private String designation; ...> public Employee(String firstName, String lastName, String designation) { ...> this.firstName = firstName; ...> this.lastName = lastName; ...> this.designation = designation; ...> } ...> public String getFirstName() { ...> return firstName; ...> } ...> public String getLastName() { ...> return lastName; ...> } ...> public String getDesignation() { ...> return designation; ...> } ...> public String toString() { ...> return "Name = " + firstName + ", " + lastName + " | " + ...> "designation = " + designation; ...> } ...> } | created class Employee jshell> Employee emp = new Employee("Sai", "Adithya", "Content Developer"); emp ==> Name = Sai, Adithya | designation = Content Developer</strong>
以上是在Java 9的JShell中,我们需要遵循哪些规则呢?的详细内容。更多信息请关注PHP中文网其他相关文章!