我在类StringSplit里面有了这个int变量,我需要将其值传递给另一个名为EndStatement的类来打印出来;尽管我认为它不能真正把它作为参数传递。我怎样才能最好地将变量放到我需要的地方?有人可以帮助提示吗?我读过Java教程,但不太明白。变量和传递它们似乎是Java编程中我的致命弱点之一。
编辑添加:parseCommands可以调用几个不同的Statement类,例如EndStatement或PrintlnStatement,这取决于从String中分析出的Array的第一个元素,该String用作一个名为commandHash的HashMap的关键字。 Statement类实现了Directive接口,该接口只有一个名为execute的方法,其参数为String[] parts。 (EndStatement implements Directive)。展开parseCommands方法以显示发生了什么。
public class StringSplit
{
public void parseCommands(String fileName)
{
//FileReader and BufferedReader to read a file with the code
//to execute line by line into an ArrayList myString
int lineCounter=0; //need to get this variable's value into class EndStatement
for (String listString: myString)
{
lineCounter++;
String[] parts=listString.trim.split("[\\s]+", 2)//split String into 2 parts
//to get commands
Directive directive= commandHash.get(parts[0])//parts[0] is the hashmap keyword
}
public class EndStatement implements Directive
{
public void execute(String[] parts)
{
//need to get lineCounter here--how?
System.out.print(lineCounter +"lines processed.");
}
public static void main (String[]args)
StringSplit ss = new StringSplit();
ss.parseCommands(args[0]);
}