Java将变量从一个类中的方法传递到不同的类

时间:2012-01-29 09:31:44

标签: java scope

我在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]);

}

3 个答案:

答案 0 :(得分:3)

这是我第一次回答问题,但我认为我是对的。

StringSplit中,您要在数据字段中声明linceCounter

public class StringSplit
{
    public void parseCommands(String fileName)
    {
      lineCounter=0; //this is the variable I need to pass into a different class
      for (String listString: myString)
      {
        lineCounter++;
        //more code here
      }
     }

     public int getLineCounter()
     {
          return lineCounter;
     }


     private int lineCounter; //this is what I call a data field, you should declare these as private as oppose to public to comply with encapsulation
}

然后在您的主要方法中调用getLinceCounter,然后将其返回的内容传递给EndStatment

这有意义吗?我理解你的问题了吗?

答案 1 :(得分:1)

public class StringSplit
{
    private int lineCounter=0; 

    public void parseCommands(String fileName)
    {

        for (String listString: myString)
        {
            lineCounter++;
            //more code here
        }
    }

    public int getLineCounter() {
       return lineCounter;
    }
}

public class EndStatement implements Directive
{
    StringSplit ss = new StringSplit();

    public void execute(String[] parts)
    {
        //need to get lineCounter here--how?
        System.out.print(ss.getLineCounter() +"lines processed.");
    }


    public static void  main (String[]args)
    {
        ss.parseCommands(args[0]);
    }
}

答案 2 :(得分:1)

我认为你混合了一些术语。没有将变量从一个类传递到另一个类的事情。我假设您想要做的只是能够在StringSplit类之外访问(设置/获取)您的变量。为此,您必须将parseCommands方法之外的lineCounter声明为StringSplit的属性。目前lineCounter是parseCommands方法的本地方法,因此无法在该方法之外显示/访问,而不能提及能够从类/对象外部访问它。那样做:

public class StringSplit
{

    public int lineCounter = 0;
    ...

现在,您将能够从同一类的不同方法和类外的方法访问lineCounter。使lineCounter公开使其他人可以完全访问它。正如'Jon'所指出的那样,它有时可能是危险的,但对于这个例子,案例是可以接受的。您可能会看到如何使用'Nurlan'的私有字段来防止来自外部的写入,其中成员仅用于提供读取访问。