[英]how to pass a variable value from one class to another
I have two packages one is com.firstBooks.series.db.parser which have a java file XMLParser.java, I have another package com.firstBooks.series79 which have a class called AppMain.NW I want to send the value of a variable called _xmlFileName frm AppMain class to the xmlFile variable in XMLParser class, I am posting the code for both the class, kindly help me. 我有两个包,一个是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 the code for AppMain class 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();
}
}
You just have to type: 您只需要输入:
XMLParser.xmlFile = AppMain._xmlFileName;
For instance here: 例如在这里:
public static void main(String args[]) {
new AppMain().enterEventDispatcher();
XMLParser.xmlFile = AppMain._xmlFileName;
}
I'm not sure if that's the best design, but address your question. 我不确定这是否是最佳设计,但请解决您的问题。
Well, the way you have it set up, one line: 好的,您的设置方式如下:
XmlParser.xmlFile = _xmlFileName;
in your AppMain somewhere, say the initialize method, should do the trick. 在您的AppMain中某个地方,比如说initialize方法,应该可以解决问题。 Maybe I'm not understanding your question.
也许我不明白您的问题。 It seems likely that you wouldn't want xmlFile to be a public static, but that's your choice.
您似乎不希望xmlFile成为公共静态变量,但这是您的选择。
Other answers are correct, but using static variables like that is a rather rigid design. 其他答案是正确的,但是使用这样的静态变量是一个比较严格的设计。 It will cause problems if you later want to use a second XMLParser object to parse a different file.
如果您以后要使用第二个XMLParser对象来解析另一个文件,它将导致问题。
Instead, the XMLParser constructor could take the file name as a parameter: 而是,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();
}
Then in some other part of the application, when you create the XMLParser: 然后在应用程序的其他部分中,当您创建XMLParser时:
XMLParser xmlParser = new XMLParser(AppMain._xmlFileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.