【问题标题】:How can I access enclosing class instance variables from inside the anonymous class?如何从匿名类内部访问封闭类实例变量?
【发布时间】:2013-04-05 01:44:36
【问题描述】:

如何从匿名类的方法中访问instance variables

class Tester extends JFrame {

   private JButton button;
   private JLabel label;
   //..some more

   public Tester() {
        function(); // CALL FUNCTION
   }

   public void function() {
      Runnable r = new Runnable() {
         @Override
         public void run() {
            // How do I access button and label from here ?
         }
      };
      new Thread(r).start();
   }
}

【问题讨论】:

标签: java anonymous-class anonymous-inner-class


【解决方案1】:

您正在寻找的是一个完全限定的地址,因为它们没有标记为final

final Runnable r = new Runnable() {
public void run() {
    Tester.this.button // access what you need
    Tester.this.label  // access what you need
}};

在构建 ActionListeners 和其他东西时,您对 Anonymous Inner Classes 使用相同的访问模式。

这在规范中被解释为15.8.4 Qualified this,反对票的人显然没有读过。也没有读懂代码。

【讨论】:

    【解决方案2】:

    如何从匿名类的方法中访问instance variables

    如果需要,您只需访问它们:

    class Tester extends JFrame {
    
       private JButton button;
       private JLabel label;
       //..some more
    
       public Tester() {
            function(); // CALL FUNCTION
       }
    
       public void function() {
          Runnable r = new Runnable() {
             @Override
             public void run() {
                System.out.println("Button's text is: " + button.getText());
             }
          };
          new Thread(r).start();
       }
    }
    

    更重要的是:为什么这对你不起作用?

    【讨论】:

    • 我在做this.label。原因!
    • @saplingPro:在这种情况下,除非存在名称冲突,否则无需使用 this 限定您的变量——两个具有相同名称的变量,一个在内部类中,一个在外部类中.在这种情况下,贾罗德的回答是正确的。但您的问题从未告诉我们您使用this 开始使其成为一个非常混乱和不完整的问题。
    【解决方案3】:

    下面的代码可以解释你的格式是IncloseingClassName.this.VariableName;

    class Tester extends JFrame {
    int abc=44;//class variable with collision in name
    int xyz=4 ;//class variable without collision in name
    public Tester() {
    function(); // CALL FUNCTION  
    }
    public void function() {
    Runnable r = new Runnable() {
    int abc=55;//anonymous class variable
         @Override
         public void run() {
          System.out.println(this.abc);  //output is 55        
          System.out.println(abc);//output is 55 
          System.out.println(Tester.this.abc);//output is 44 
          //System.out.println(this.xyz);//this will give error
          System.out.println(Tester.this.xyz);//output is 4 
          System.out.println(xyz);//output is 4 
                //output is 4 if variable is declared in only enclosing method 
                //then there is no need of Tester.this.abcd ie you can directly 
                //use the variable name if there is no duplication 
                //ie two variables with same name hope you understood :)
    
         }
      };
      new Thread(r).start();
       }  }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多