Java 如何将变量值从一个类传递到另一个类

Java 如何将变量值从一个类传递到另一个类,java,Java,我有两个包,一个是com.firstBooks.series.db.parser,它有一个java文件XMLParser.java,另一个是com.firstBooks.series79,它有一个名为AppMain.NW的类,我想把一个名为_xmlfilenamefrm AppMain的变量的值发送到XMLParser类中的xmlFile变量,我正在发布这两个类的代码,请帮帮我 package com.firstBooks.series.db.parser; import java.io.IO

我有两个包,一个是com.firstBooks.series.db.parser,它有一个java文件XMLParser.java,另一个是com.firstBooks.series79,它有一个名为AppMain.NW的类,我想把一个名为_xmlfilenamefrm AppMain的变量的值发送到XMLParser类中的xmlFile变量,我正在发布这两个类的代码,请帮帮我

package com.firstBooks.series.db.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.firstBooks.series.db.Question;

public class XMLParser {

 private Document document;
 public static Vector questionList;
 public static String xmlFile;

 public XMLParser() {     
  questionList = new Vector();
 }


 public void parseXMl() throws SAXException, IOException,
   ParserConfigurationException {

  // Build a document based on the XML file.
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputStream inputStream = getClass().getResourceAsStream(xmlFile);

  document = builder.parse(inputStream);
 }

 public void parseDocument() {
  Element element = document.getDocumentElement();

  NodeList nl = element.getElementsByTagName("question");

  if (nl != null && nl.getLength() > 0) {
   for (int i = 0; i < nl.getLength(); i++) {
    Element ele = (Element) nl.item(i);
    Question question = getQuestions(ele);
    questionList.addElement(question);
   }
  }
 }

 private Question getQuestions(Element element) {

  String title = getTextValue(element, "title");
  String choice1 = getTextValue(element, "choice1");
  String choice2 = getTextValue(element, "choice2");
  String choice3 = getTextValue(element, "choice3");
  String choice4 = getTextValue(element, "choice4");
  String answer = getTextValue(element, "answer");
  String rationale = getTextValue(element, "rationale");

  Question Questions = new Question(title, choice1,
    choice2, choice3, choice4, answer, rationale);

  return Questions;
 }

 private String getTextValue(Element ele, String tagName) {
  String textVal = null;
  NodeList nl = ele.getElementsByTagName(tagName);
  if (nl != null && nl.getLength() > 0) {
   Element el = (Element) nl.item(0);
   textVal = el.getFirstChild().getNodeValue();
  }

  return textVal;
 }
}
您只需键入:

 XMLParser.xmlFile = AppMain._xmlFileName;
例如:

public static void main(String args[]) {
  new AppMain().enterEventDispatcher();
  XMLParser.xmlFile = AppMain._xmlFileName;
}

我不确定这是否是最好的设计,但请回答您的问题

好吧,按照您的设置方式,一行:

XmlParser.xmlFile = _xmlFileName;

在AppMain中的某个地方,比如initialize方法,应该可以实现这一点。也许我不明白你的问题。似乎您不希望xmlFile成为公共静态文件,但这是您的选择。

其他答案是正确的,但使用这样的静态变量是一种相当严格的设计。如果以后要使用第二个XMLParser对象来解析不同的文件,则会导致问题

相反,XMLParser构造函数可以将文件名作为参数:

public class XMLParser {

  private Document document;
  public static Vector questionList;
  private static String xmlFile;  // this can be private

  public XMLParser(String xmlFile) {
    this.xmlFile = xmlFile;
    questionList = new Vector();
  }
然后在应用程序的其他部分中,当您创建XMLParser时:

XMLParser xmlParser = new XMLParser(AppMain._xmlFileName);
XMLParser xmlParser = new XMLParser(AppMain._xmlFileName);