简体   繁体   English

如何从匿名类中访问封闭的类实例变量?

[英]How can I access enclosing class instance variables from inside the anonymous class?

How do I access instance variables from inside the anonymous class's method ? 如何从匿名类的方法中访问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();
   }
}

What you are looking for is a fully qualified address since they are not marked final 你要找的是一个完全合格的地址,因为它们没有标记为final

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

You use the same access pattern for Anonymous Inner Classes when building ActionListeners and other things as well. 在构建ActionListeners和其他东西时,您对Anonymous Inner Classes使用相同的访问模式。

This is explained in the specifications as 15.8.4 Qualified this , something the down voter apparently hasn't read. 这在规范中被解释为15.8.4合格的 ,这是下选民显然没有阅读的内容。 And didn't read the code for comprehension either. 并没有阅读理解代码。

the following code may explain you the format is IncloseingClassName.this.VariableName; 以下代码可以解释你的格式是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();
   }  }

How do I access instance variables from inside the anonymous class's method ? 如何从匿名类的方法中访问instance variables

You simply access them if need be: 如果需要,您只需访问它们:

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();
   }
}

More important: Why isn't this working for you? 更重要的是:为什么这不适合你?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何引用在Java中封装匿名类实例? - How to refer to enclosing anonymous class instance in Java? 在静态工厂方法中使用覆盖方法创建实例时,如何访问封闭类中的私有字段? - How can I access private field in enclosing class when creating instance with overridden method in static factory method? 在匿名类中访问Enclosing类 - Accessing Enclosing Class inside anonymous class 定义为匿名内部类的Java事件侦听器如何使用封闭类中的变量? - How can an java event listener defined as an anonymous inner class use a variable from an enclosing class? 从匿名类参数访问类的实例 - Access an instance of a class from anonymous class argument 如何从封闭的外部 class 调用同名方法作为在匿名 class 中实现的方法? - How do I call a method of same name from an enclosing outer class as the one being implemented in an anonymous class? 请参阅匿名内部类封闭类'this - Refer to enclosing class' this from anonymous inner class 从匿名 class 内部访问方法 scope 中的变量 - Access variables in method scope from inside anonymous class 为什么匿名类可以访问封闭类的非final类成员 - Why can an anonymous class access non-final class member of the enclosing class 当从实例方法返回一个没有引用其封闭类的匿名类时,它会引用它。为什么? - When an anonymous class with no references to its enclosing class is returned from an instance method, it has a reference to this. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM