编写一个解释文本文件中一行的程序。
想知道我是否应该将方法"parseWordData"中的局部变量命名为scannedWord
与word
相反,因为word
已经是一个类字段。
只要我声明一个新变量而不是重新分配旧变量,一切都应该没问题......右?
public class WordData {
private String word;
private int index;
private LinkedList<Integer> list;
private boolean pathFound;
public WordData(String word, int index, LinkedList<Integer> list, boolean pathFound) {
this.word = word;
this.index = index;
this.list = list;
this.pathFound = pathFound;
}
public WordData parseWordData(String line){
Scanner scan = new Scanner(line);
int index = scan.nextInt();
String word = scan.next();
//precond and subGoal
LinkedList<Integer> list = new LinkedList<Integer>();
while(scan.hasNextInt()){
//add to LinkedList
}
return new WordData(word, index, list, false)
}
不要担心逻辑,我只想知道这样命名的东西是否会令人困惑或在 Java 中是禁忌
在 Java 中,标准做法是将参数命名为与
- 构造 函数
- 二传手方法
前提是这些方法将字段设置为与参数具有相同的值。 在这种情况下,您会看到类似
this.word = word;
在构造函数或 setter 方法中。
在所有其他情况下,应避免使用与字段名称相同的参数名称或局部变量名称。 这只会导致混乱。 这是标准做法。
因此,在您的示例中,是的,您应该对从输入扫描的单词使用 scannedWord
或类似的东西。