我正在使用 Red Hat 插件对 Java(TM) 的语言支持的 Visual Studio 代码。我已将 settings.json 中的运行时设置为
"java.configuration.runtimes": [
{
"name": "JavaSE-17",
"path": "path/to/jdk19",
"default": true
}
]
以下独立 java 文件
import java.util.Objects;
public class Edge {
public final int first;
public final int second;
public Edge(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object other) {
return switch (other) {
case Edge e -> (first == e.first && second == e.second) || (first == e.second && second == e.first);
default -> false;
};
}
@Override
public int hashCode() {
if (first < second)
return Objects.hash(first, second);
else
return Objects.hash(second, first);
}
}
给出以下错误:Cannot switch on a value of type Object. Only convertible int values, strings or enum variables are permitted
, Edge cannot be resolved to a variable
, Syntax error on token "e", delete this token
, e cannot be resolved to a variable
.
IntelliJ 没有显示任何错误,所以我认为代码不正确。我已经测试了sealed
java 17 中的关键字是否有效。尽管 switch 的模式匹配是一项预览功能,但我听说独立文件的预览功能默认启用。我还看到有人在 vscode 中使用此功能的视频。我该如何使用该功能?