Paths.get与Path.of [英] Paths.get vs Path.of

查看:1084
本文介绍了Paths.get与Path.of的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Paths.getPath.of似乎做的完全一样,将一个或多个字符串转换为Path对象;文档

As far as I can tell, Paths.get and Path.of seem to do exactly the same thing, turning one or more strings into a Path object; the documentation https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html#get-java.lang.String-java.lang.String...- and https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/Path.html#of(java.lang.String,java.lang.String...) use the same wording. Are they in fact identical?

Path.of稍后介绍.猜想:引入它是为了保持一致的Foo.of样式.在那种情况下,出于一致性/审美的考虑,它会被认为是更可取的吗?

Path.of was introduced later. Conjecture: it was introduced for the sake of a consistent Foo.of style. In that case, it would be considered preferable on consistency/aesthetic grounds?

推荐答案

实际上,后来引入了Path.of.

Indeed, Path.of was later introduced.

猜想:引入它是为了保持一致的Foo.of风格.

从邮件列表存档中,此方法曾经是称为Path.get :

From the mailing list archive, this method was once called Path.get:

主要的更改是在java.nio.file中的路径"和路径"中.

The main changes in are in Path and Paths in java.nio.file.

此修补程序将Paths.get()方法复制到Path.get()中的静态方法,并修改前者以调用后者各自的方法.略微清理了路径规范,使其既不引用路径也不引用路径本身,例如(请参阅路径)". @implSpec批注已添加到Paths,以指示这些方法只是在Path中调用它们的对应方法.
...

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建议将其更改为与Foo.of 一致:

This was later changed when Brian Goetz suggested it to be consistent with Foo.of:

另外,布莱恩·格茨(Brian Goetz)建议将不在名单上的建议 如果这些工厂方法被命名为"of",则保持一致,因此我假设 webrev将被更新以查看外观.

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.

现在,您的最后一个问题是:在那种情况下,从一致性/美学角度考虑,它会更可取吗?"
Brian Burkhalter在初始邮件中说,他在Path中更新了对新方法的所有引用:

Now to your last question: "In that case, it would be considered preferable on consistency/aesthetic grounds?"
In the initial mail Brian Burkhalter said that he updated all references to the new method in Path:

修改了java.base中的所有源文件,以将Paths.get()更改为Path.get()并删除Paths的导入. ...

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 ,您会发现此注释:

So I would therefore conclude that Path.of is indeed preferable to Paths.get.
Indeed, if you look at the Javadoc for Paths for Java 13 you will find this note:

API注释:
建议通过Path.of方法而不是通过此类中定义的get方法获取Path,因为在将来的版本中可能不推荐使用该类.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆