gpt4 book ai didi

java - 关于 jls 一点的澄清

转载 作者:行者123 更新时间:2023-11-30 07:15:06 26 4
gpt4 key购买 nike

直接来自这个jls :

A try-with-resources statement with a ResourceSpecification clause that declares multiple resources is treated as if it were multiple try-with-resources statements, each of which has a ResourceSpecification clause that declares a single Resource. When a try-with-resources statement with n Resources (n > 1) is translated, the result is a try-with-resources statement with n-1 Resources. After n such translations, there are n nested try-catch-finally statements, and the overall translation is complete.

对我来说,上述声明的最后一部分非常清楚。为什么如果我声明了 n 个资源,那么正如他们所说,结果是带有 n-1 个资源的 try-with-resources 语句

为什么不使用 n 个资源?

提前致谢。

最佳答案

JLS 试图说的是,如果您有:

try (
Closable closeable1 = ...;
Closable closeable2 = ...;
// ...
Closable closeablen = ...;
) {
// ...
}

它将被反复分解为:

try (Closable closeable1 = ...) {
try (
Closable closeable2 = ...;
// ...
Closable closeablen = ...;
) {
// ...
}
}

只要 try 中有多个资源,就会重复此过程。这只是一个示例,因为我假设它已分解为用于 try-with-resources 的实际字节码。

关于java - 关于 jls 一点的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com