我的教科书将 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/