Java 的新 Scanner 与 Scanner.create()?

发布于 2024-12-29 18:04:51 字数 165 浏览 0 评论 0 原文

我在 O'Reillys Java Cookbook (2ed) 中查找一些好东西,并发现 Scanner.create() 方法大约 10 次。但 API 或类声明/实现中没有这样的内容。例如:页面示例

I was looking through O'Reillys Java Cookbook (2ed) for some good stuff and found Scanner.create() method about 10 times. But there's no such in the API or class declaration\implementation. Ex: Page example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

壹場煙雨 2025-01-05 18:04:51

它要么指的是非 SDK 扫描仪类型,要么是书中的错误。

Java 1.5/5(引入时),或在 SDK 6 SDK 7 API。也没有提到这种方法已被废弃(在 Java SDK API 中,这实际上意味着它从未[正式]存在:-)。

更新说明:看来 create 工厂方法确实存在于 Java 5 的最早预览版/测试版中。RanRag 发现了一个 有关该问题的相关帖子

...不,你不会发疯:扫描仪在 [Java 5] Tiger-beta1 中具有 create() 方法,但它们切换到了 [Java 5] 中的构造函数] beta2

(所以唯一正确的方法是使用构造函数。)

快乐编码。

It's either referring to a non-SDK Scanner type or is an error in the book.

There is no static Scanner Scanner.create() in Java 1.5/5 (when it was introduced), or in the SDK 6 or SDK 7 APIs. There is also no mention of such a method being obsoleted (which, in Java SDK API, effectively means it never [officially] existed :-).

Update note: It appears that the create factory method did indeed exist in the earliest preview/beta versions of Java 5. RanRag found a relevant thread on the issue:

...and no, you aren't going crazy: Scanner had create() methods in [Java 5] tiger-beta1, but they switched to constructors in [Java 5] beta2.

(So the only correct way to is use the constructor.)

Happy coding.

━╋う一瞬間旳綻放 2025-01-05 18:04:51

Oracle 论坛中提到 Scanner 在 Tiger 中有 create() 方法-beta1,但他们在 beta2 中切换到构造函数。

这不再是这样做的方法。您应该创建一个实例
Scanner 类的创建方式与创建任何 Scanner 类的实例的方式相同
其他类,通过使用构造函数。

Scanner sc  = new Scanner(System.in)

It is mentioned in Oracle forums that Scanner had create() methods in tiger-beta1, but they switched to constructors in beta2.

This is no longer the way to do it. You should create an instance of
the Scanner class in the same way you would create an instance of any
other class, by using a constructor.

Scanner sc  = new Scanner(System.in)
柒夜笙歌凉 2025-01-05 18:04:51

嗯,看起来有人在食谱发布之前没有检查更新的规格!实例化 Scanner 对象的唯一方法是:

Scanner sc  = new Scanner(System.in)

根据 API文档

Hmm looks like someone did not check the updated spec before the cookbook post! The only way to instatiate a Scanner objects is:

Scanner sc  = new Scanner(System.in)

according to the API documentation

路还长,别太狂 2025-01-05 18:04:51

作为被引用作品的作者,我对基于 Tiger (1.5) Beta 的写作所造成的不便表示歉意。令人惊讶的是,我错过了一个不正确的示例(Scanner sc = Scanner.create(System.in);),并且该示例一直贯穿第三版和第四版,但没有人报告它O'Reilly 勘误网站,技术评论家也没有发现它。嗯,第五版已经修复了,现在正在准备中!

As the author of the cited work, I apologize for the inconvenience caused by my writing that based on a Tiger (1.5) Beta. What is surprising is that I missed one incorrect example (Scanner sc = Scanner.create(System.in);) and that made it all the way through the 3rd and 4th edition, and nobody reported it on the O'Reilly errata site, nor did the tech reviewers catch it. Well, it's being fixed in the fifth edition, in preparation now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文