简体   繁体   English

在方法之间传递值

[英]Passing values between methods

I've got method where I read value into variable我有将值读入变量的方法

public void displayFromExcel(String xlsPath) {
. 
. 
. 
pole[i] = cell.getNumericCellValue();
.
.
pole1[j] = richTextString;

Then I have method where I build a String using StringBuilder然后我有使用StringBuilder构建String的方法

    private void getHenkValues (StringBuilder sb) { 
    sb.append("<ColumnValue name=\"hen_allockey\">" + pole1[j] + "</ColumnValue\">\r\n"
            +"<ColumnValue name=\"hen_percentage\">"+ pole[i] + "</ColumnValue\">\r\n");
}

Then I have method where I write it into file:然后我有将它写入文件的方法:

protected void jobRun() throws Exception {
sb = new StringBuilder();
getHenkValues(sb);
String epilog1 = sb.toString();

FileOutputStream fos = new FileOutputStream("c:\\test\\osem.xml");
OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
osw.write(epilog1);
osw.flush();
osw.close();  
}

And in method main I call the method jobrun .main方法中,我将方法jobrun

How can I get the values from pole[i] , pole1[j] from method displayFromExcel to method getHenkValues ?如何从方法displayFromExcel到方法getHenkValuespole[i]pole1[j]获取值?

Your displayFromExcel method need to return them (using a custom class or a collection of some sort, perhaps an array).您的displayFromExcel方法需要返回它们(使用自定义 class 或某种集合,可能是数组)。

Your getHenkValues needs to accept these values as well, you could try something like:您的getHenkValues需要接受这些值,您可以尝试以下操作:

getHenkValues(StringBuilder sb, Object value1, Object value2)

or whatever is relevant for your case.或与您的情况相关的任何内容。

You could make pole and pole1 private fields of the class in which displayFromExcel, getHenkValues and jobRun are located:您可以设置 displayFromExcel、getHenkValues 和 jobRun 所在的 class 的pole 和pole1 私有字段:

private Object[] pole;
private String[] pole1;

Then you can assign values to these arrays in one method and access them in another.然后,您可以用一种方法为这些 arrays 赋值,并用另一种方法访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM