1. <small id='I5eb0'></small><noframes id='I5eb0'>

        <legend id='I5eb0'><style id='I5eb0'><dir id='I5eb0'><q id='I5eb0'></q></dir></style></legend>
        • <bdo id='I5eb0'></bdo><ul id='I5eb0'></ul>

        <i id='I5eb0'><tr id='I5eb0'><dt id='I5eb0'><q id='I5eb0'><span id='I5eb0'><b id='I5eb0'><form id='I5eb0'><ins id='I5eb0'></ins><ul id='I5eb0'></ul><sub id='I5eb0'></sub></form><legend id='I5eb0'></legend><bdo id='I5eb0'><pre id='I5eb0'><center id='I5eb0'></center></pre></bdo></b><th id='I5eb0'></th></span></q></dt></tr></i><div id='I5eb0'><tfoot id='I5eb0'></tfoot><dl id='I5eb0'><fieldset id='I5eb0'></fieldset></dl></div>

      2. <tfoot id='I5eb0'></tfoot>

        Java - 如何更改“本地"?事件侦听器中的变量

        时间:2023-06-28
          <bdo id='xX7Cw'></bdo><ul id='xX7Cw'></ul>
        • <tfoot id='xX7Cw'></tfoot>

            <small id='xX7Cw'></small><noframes id='xX7Cw'>

            1. <legend id='xX7Cw'><style id='xX7Cw'><dir id='xX7Cw'><q id='xX7Cw'></q></dir></style></legend>
                <tbody id='xX7Cw'></tbody>

                  <i id='xX7Cw'><tr id='xX7Cw'><dt id='xX7Cw'><q id='xX7Cw'><span id='xX7Cw'><b id='xX7Cw'><form id='xX7Cw'><ins id='xX7Cw'></ins><ul id='xX7Cw'></ul><sub id='xX7Cw'></sub></form><legend id='xX7Cw'></legend><bdo id='xX7Cw'><pre id='xX7Cw'><center id='xX7Cw'></center></pre></bdo></b><th id='xX7Cw'></th></span></q></dt></tr></i><div id='xX7Cw'><tfoot id='xX7Cw'></tfoot><dl id='xX7Cw'><fieldset id='xX7Cw'></fieldset></dl></div>
                  本文介绍了Java - 如何更改“本地"?事件侦听器中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有个小问题,希望有人能回答.基本上我有一个字符串变量,它需要根据组合框中的值进行更改,该组合框中附加了一个事件侦听器.但是,如果我将字符串设为 final,则无法更改,但如果我不将其设为 final,则 eclipse 会抱怨它不是最终的.最好(和最简单)的解决方法是什么?

                  Got a quick question which I hope someone can answer. Basically I have a String variable which needs changing based upon the value in a combo box which has an event listener attached to it. However if I make the string final then it cant be changed, but if i don't make it final then eclipse moans that it isn't final. Whats the best (and simplest) work around?

                  代码如下

                  final String dialogOutCome = "";
                  
                  //create a listener for the combo box
                  Listener selection = new Listener() {
                  
                      public void handleEvent(Event event) {
                      //get the value from the combo box
                      String comboVal = combo.getText();
                  
                      switch (comboVal) {
                           case "A": dialogOutCome = "a";
                           case "B": dialogOutCome = "b";
                           case "C": dialogOutCome = "c";
                           case "D": dialogOutCome = "d";
                      }
                     }
                  };
                  

                  推荐答案

                  你不能.

                  考虑一下:

                  • 只要声明的方法运行,局部变量就存在.
                  • 只要方法调用结束(通常是因为方法存在),变量就会消失
                  • 听众可以(而且通常确实)活得更久

                  那么当方法已经返回并且监听器尝试修改局部变量时会发生什么?

                  So what should happen when the method returned already and the listener tries to modify the local variable?

                  因为这个问题没有很好的答案,他们决定通过不允许访问非final 局部变量来使这种情况变得不可能.

                  Because this question does not have a really good answer, they decided to make that scenario impossible by not allowing access to non-final local variables.

                  有两种方法可以解决这个问题:

                  There are two ways around this problem:

                  • 尝试更改字段而不是局部变量(这可能也更适合监听器的生命周期)
                  • 使用 final 局部变量,您可以更改其中的 content(例如 ListString[] 使用单个元素).
                  • try to change a field instead of a local variable (this probably also fits better with the life-time of the listener) or
                  • use a final local variable, of which you can change the content (for example a List or a String[] with a single element).

                  这篇关于Java - 如何更改“本地"?事件侦听器中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:没有任何事件发生 下一篇:使用 Lucene 的精确短语搜索?

                  相关文章

                  <small id='uDrLd'></small><noframes id='uDrLd'>

                    <bdo id='uDrLd'></bdo><ul id='uDrLd'></ul>

                      <tfoot id='uDrLd'></tfoot>
                      <i id='uDrLd'><tr id='uDrLd'><dt id='uDrLd'><q id='uDrLd'><span id='uDrLd'><b id='uDrLd'><form id='uDrLd'><ins id='uDrLd'></ins><ul id='uDrLd'></ul><sub id='uDrLd'></sub></form><legend id='uDrLd'></legend><bdo id='uDrLd'><pre id='uDrLd'><center id='uDrLd'></center></pre></bdo></b><th id='uDrLd'></th></span></q></dt></tr></i><div id='uDrLd'><tfoot id='uDrLd'></tfoot><dl id='uDrLd'><fieldset id='uDrLd'></fieldset></dl></div>
                      <legend id='uDrLd'><style id='uDrLd'><dir id='uDrLd'><q id='uDrLd'></q></dir></style></legend>