簡體   English   中英

在方法之間傳遞值

[英]Passing values between methods

我有將值讀入變量的方法

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

然后我有使用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");
}

然后我有將它寫入文件的方法:

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();  
}

main方法中,我將方法jobrun

如何從方法displayFromExcel到方法getHenkValuespole[i]pole1[j]獲取值?

您的displayFromExcel方法需要返回它們(使用自定義 class 或某種集合,可能是數組)。

您的getHenkValues需要接受這些值,您可以嘗試以下操作:

getHenkValues(StringBuilder sb, Object value1, Object value2)

或與您的情況相關的任何內容。

您可以設置 displayFromExcel、getHenkValues 和 jobRun 所在的 class 的pole 和pole1 私有字段:

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

然后,您可以用一種方法為這些 arrays 賦值,並用另一種方法訪問它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM