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

标签 java

我有两个包,一个是 com.firstBooks.series.db.parser,它有一个 java 文件 XMLParser.java,我有另一个包 com.firstBooks.series79,它有一个名为 AppMain.NW 的类,我想发送该值将名为 _xmlFileName frm 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;
 }
}

Nw AppMain 类的代码

//#preprocess
package com.firstBooks.series79;

import net.rim.device.api.ui.UiApplication;

import com.firstBooks.series.ui.screens.HomeScreen;

public class AppMain extends UiApplication {

 public static String _xmlFileName; 
 public static boolean _Lite; 
 public static int _totalNumofQuestions;

 public static void initialize(){
   //#ifndef FULL  
     /* 
     //#endif 
        _xmlFileName = "/res/Series79_FULL.xml";
        _totalNumofQuestions = 50;
        _Lite = false;
     //#ifndef FULL 
     */  
  //#endif 

  //#ifndef LITE  
     /* 
     //#endif 
       _xmlFileName = "/res/Series79_LITE.xml";
       _totalNumofQuestions = 10;
       _Lite = true;
     //#ifndef LITE 
     */  
   //#endif 
  }


 private AppMain() {
  initialize();
  pushScreen(new HomeScreen());
 }

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

最佳答案

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

XmlParser.xmlFile = _xmlFileName;

在你的 AppMain 的某个地方,比如说初始化方法,应该可以解决问题。也许我不明白你的问题。您似乎不希望 xmlFile 成为公共(public)静态,但这是您的选择。

关于java - 如何将变量值从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3026862/

相关文章:

java - 如何在Java中多次打印一个对象?

javascript - 如何在后台运行cocos Creator的android构建

java - 使用 Liferay 的 DynamicQuery 按另一个表排序

java - OutOfMemoryError 通过使用 java.util.Timer

java - GridBagLayOut 向上跨越列?

java - 接口(interface)中的嵌套类

java - Vector容量的目的是什么

java - java接口(interface)如何调用对象类的方法

java - 引用类型和对象类型改变 Java

java - 使用 BorderLayout 绘制到嵌套面板