java将不同类的变量组合_Java将变量从一个类中的方法传递到不同的类中

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值