使用JDK/12 EarlyAccess Build 10,JEP-325开关表达式已集成为JDK中的预览功能。表达式的示例代码(同样在JEP中):
Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next());
switch (day) {
case MONDAY, TUESDAY -> System.out.println("Back to work.") ;
case WEDNESDAY -> System.out.println("Wait for the end of week...") ;
case THURSDAY,FRIDAY -> System.out.println("Plan for the weekend?");
case SATURDAY, SUNDAY -> System.out.println("Enjoy the holiday!");
}
其中Day
为枚举,>]
public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
Preview Language and VM Features JEP-12已经详细说明了如何在编译和运行时使用
javac
和java
启用功能。
如何使用Maven尝试此功能?
在JDK / 12 EarlyAccess Build 10中,JEP-325开关表达式已集成为JDK的预览功能。表达式的示例代码(同样在JEP中):扫描程序扫描程序=新...
要启用预览功能,必须在compilerArgs下的pom.xml中定义--enable-preview