0

我想在 Class中访问变量sTerminalId&sTransactionIdServerThread

public class ServerThread extends Thread {

    /* Some Code Omitted Here */

    public void run() {
            String sDataFromPOS="";
            //Get Some Value in sDataFromPOS
            byte[] bSendToPOS = SAXHandler.ParseXMLToString(sDataFromPOS.toString().substring(2));
    }
}

public class SAXHandler extends DefaultHandler {

    static String sTerminalId = "";
    static String sTransactionId = "";

    public static byte[] ParseXMLToString(String sXMLData) throws Throwable {
        /* Do Some Operation on String sXMLData & extarct value of sTerminalId & sTransactionId*/
    }
}

我尝试的是

SAXHandler.sTerminalId

会给我带来价值,但如果再次运行它只会给我一次运行它给我以前的价值。有什么选择吗?我正在处理敏感数据,所以不想弄乱我的代码

4

2 回答 2

5

这就是静力学不好的原因。使变量成为实例变量并为每次运行创建一个新的对象实例(为简单起见,我们忽略封装):

public class ServerThread extends Thread {
    public void run() {
        String sDataFromPOS="";
        //Get Some Value in sDataFromPOS
        SAXHandler handler = new SAXHandler();
        byte[] bSendToPOS = handler.parseXMLToString(sDataFromPOS.toString().substring(2));
        handler.sTerminalId .... // access instance variables
    }
}

public class SAXHandler extends DefaultHandler {
    String sTerminalId = "";
    String sTransactionId = "";

    public byte[] parseXMLToString(String sXMLData) throws Throwable {
        /* Do Some Operation on String sXMLData & extarct value of sTerminalId & sTransactionId*/
    }
}
于 2013-08-29T11:20:47.467 回答
0

在处理之前再次重新初始化您的变量......比如

public static byte[] ParseXMLToString(String sXMLData) throws Throwable {

     sTerminalId = "";
     sTransactionId = "";

    }
于 2013-08-29T11:25:29.063 回答