我在 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
发布评论
评论(4)
它要么指的是非 SDK 扫描仪类型,要么是书中的错误。
Java 1.5/5(引入时),或在 SDK 6 或 SDK 7 API。也没有提到这种方法已被废弃(在 Java SDK API 中,这实际上意味着它从未[正式]存在:-)。
更新说明:看来
create
工厂方法确实存在于 Java 5 的最早预览版/测试版中。RanRag 发现了一个 有关该问题的相关帖子:(所以唯一正确的方法是使用构造函数。)
快乐编码。
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:(So the only correct way to is use the constructor.)
Happy coding.
Oracle 论坛中提到 Scanner 在 Tiger 中有 create() 方法-beta1,但他们在 beta2 中切换到构造函数。
It is mentioned in Oracle forums that Scanner had create() methods in tiger-beta1, but they switched to constructors in beta2.
嗯,看起来有人在食谱发布之前没有检查更新的规格!实例化 Scanner 对象的唯一方法是:
根据 API文档
Hmm looks like someone did not check the updated spec before the cookbook post! The only way to instatiate a Scanner objects is:
according to the API documentation
作为被引用作品的作者,我对基于 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!