Paths.get 对比 Path.of
Paths.get vs Path.of
据我所知,Paths.get
和 Path.of
似乎在做完全相同的事情,将一个或多个字符串转换为 Path
对象; Paths.get
and Path.of
的文档使用相同的措辞。它们实际上是相同的吗?
Path.of
稍后介绍
猜想:为了Foo.of
风格一致而引入。在那种情况下,根据 consistency/aesthetic 理由,它会被认为更可取?
的确,Path.of
是后来引入的。
Conjecture: it was introduced for the sake of a consistent Foo.of
style.
来自邮件列表存档,这个 method was once called Path.get
:
The main changes in are in Path and Paths in java.nio.file.
This patch copies the Paths.get() methods to static methods in Path.get() and modifies the former to call the latter respective methods. The Path specification is slightly cleaned up not to refer to Paths nor itself, e.g., “(see Path).” @implSpec annotations are added to Paths to indicate that the methods simply call their counterparts in Path.
...
这后来在 Brian Goetz suggested it to be consistent with Foo.of
时更改:
Separately, Brian Goetz suggested off-list that it would be more
consistent if these factory methods were named "of" so I assume the
webrev will be updated to see how that looks.
现在回答你的最后一个问题:"In that case, it would be considered preferable on consistency/aesthetic grounds?"
在 initial mail 中,Brian Burkhalter 说他更新了 Path
中对新方法的所有引用:
All source files in java.base are modified to change Paths.get() to Path.get() and to remove the import for Paths. ...
因此我得出结论 Path.of
确实比 Paths.get
更可取。
实际上,如果您查看 Javadoc for Paths
for Java 13,您会发现这条注释:
API Note:
It is recommended to obtain a Path
via the Path.of
methods instead of via the get
methods defined in this class as this class may be deprecated in a future release.
据我所知,Paths.get
和 Path.of
似乎在做完全相同的事情,将一个或多个字符串转换为 Path
对象; Paths.get
and Path.of
的文档使用相同的措辞。它们实际上是相同的吗?
Path.of
稍后介绍
猜想:为了Foo.of
风格一致而引入。在那种情况下,根据 consistency/aesthetic 理由,它会被认为更可取?
的确,Path.of
是后来引入的。
Conjecture: it was introduced for the sake of a consistent
Foo.of
style.
来自邮件列表存档,这个 method was once called Path.get
:
The main changes in are in Path and Paths in java.nio.file.
This patch copies the Paths.get() methods to static methods in Path.get() and modifies the former to call the latter respective methods. The Path specification is slightly cleaned up not to refer to Paths nor itself, e.g., “(see Path).” @implSpec annotations are added to Paths to indicate that the methods simply call their counterparts in Path.
...
这后来在 Brian Goetz suggested it to be consistent with Foo.of
时更改:
Separately, Brian Goetz suggested off-list that it would be more consistent if these factory methods were named "of" so I assume the webrev will be updated to see how that looks.
现在回答你的最后一个问题:"In that case, it would be considered preferable on consistency/aesthetic grounds?"
在 initial mail 中,Brian Burkhalter 说他更新了 Path
中对新方法的所有引用:
All source files in java.base are modified to change Paths.get() to Path.get() and to remove the import for Paths. ...
因此我得出结论 Path.of
确实比 Paths.get
更可取。
实际上,如果您查看 Javadoc for Paths
for Java 13,您会发现这条注释:
API Note:
It is recommended to obtain aPath
via thePath.of
methods instead of via theget
methods defined in this class as this class may be deprecated in a future release.