java - Scanner 类的 nextInt 方法是 Java 中的访问器还是修改器?

标签 java methods java.util.scanner accessor mutators

我的教科书将 mutator 方法定义为修改其操作的对象的方法。

并且,它将访问器方法定义为一种在不更改对象的情况下查询对象某些信息的方法。

我说 nextInt 是访问器方法正确吗?

我的理由是 nextInt 不会修改它所操作的对象。如果我创建了一个调用的 Scanner 对象,当我写“int a = in.nextInt();”时它不会修改实际的 Scanner 对象。相反,它被用作一种 getter 方法来为 a 赋值。

最佳答案

实际上,来自 javadoc:

public int nextInt(int radix)

Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.

...

这表明扫描仪的状态在调用该方法后发生改变。所以这使得 nextInt 成为一个更改器(mutator)。

因此,nextInt 既是一个增变器又是一个访问器。

关于java - Scanner 类的 nextInt 方法是 Java 中的访问器还是修改器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066601/

相关文章:

java - 在 Linux 服务器上的命令提示符下运行 Jar 文件

java - Android Studio firebase 数据库在添加新值之前等待读取

jquery - 基于 Modernizr : click vs hover 的不同方法

java - Java 中的 OpenFile 对话框给出 null 作为响应

java - 检测扫描仪是否没有下一个

Java Android getApplicationContext() 从上一个类返回一个 null 对象

java - 在Android中,不应该使用System.gc()吗?

c# - 你如何在 C#/csharp 中返回 2 个变量?

PHPStorm - 找不到内部对象的方法

java - 使用扫描仪处理用户输入