[英]How do I access an outer variable within inner nested methods?
I am aware that this question may already exist, and this is my first question on the site, so please bear with me.I still have difficulty understanding this in my case. 我知道这个问题可能已经存在,这是我在网站上遇到的第一个问题,请耐心等待。我仍然很难理解这个问题。
So here is the thing: I have a method that I call , and I have a button that will change the value of x. 事情就是这样:我有一个我调用的方法,还有一个将改变x值的按钮。 Depending on the value of x, I want the program to do something. 根据x的值,我希望程序执行某些操作。 The program below is not very complete but you will get the idea: 下面的程序不是很完整,但是您会明白的:
public class foo{
private void do(){
int x=0;
JButton changeValue= new JButton("Change the value of x");
changeValue.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
x=10; //change the x value when the button is clicked
//Here the user may also change the value of x
//by inputting some other number
}
});
//Something happens depending on x
//But nothing happens here because when I get the value of x,
//it reverts back to 0.
}
}
However, no matter where I declare my x in do(), I keep getting an error telling me that inner class cannot access outer class variables and that they must be declared final. 但是,无论我在do()中的何处声明x,我都会不断收到错误消息,告诉我内部类不能访问外部类变量,并且必须将它们声明为final。 But I can't declare it final because I need to change it later. 但是我无法将其声明为最终的,因为我需要稍后对其进行更改。 I have tried putting the values in a new class. 我尝试将值放在新的类中。 I have also tried declaring x as a member of foo() but that results to x being 0 or null because for some reason once it exits the button click method it takes x to back to its old value: 0 or null. 我也尝试过将x声明为foo()的成员,但是导致x为0或null,因为由于某种原因,一旦退出按钮click方法,它将x返回其旧值:0或null。
What I need : when the button is pressed, the value of x is changed (Assuming that the user can change the value of x to some other number) Thanks for any answers in advance. 我需要的是:当按下按钮时,x的值会更改(假设用户可以将x的值更改为其他数字)感谢您提前提出的答案。
You need to create a final
reference to your x
variable. 您需要为x
变量创建final
引用。
Since it is primitive type, create a class to wrap it: 因为它是原始类型,所以创建一个类来包装它:
private static class MyX {
int x;
// + getter and setter
}
And then: 接着:
final MyX myX = new MyX(x);
JButton changeValue= new JButton("Change the value of x");
changeValue.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
myX.setX(10);
});
// get the value of MyX back if necessary
The problem is: when you create that ActionListener you are declaring an actionPerformed that will be executed sometime later. 问题是:创建该ActionListener时,您将声明一个actionPerformed,它将在以后的某个时间执行。 You cannot change x from actionPerformed because x exists only inside that method call. 您无法从actionPerformed更改x,因为x仅存在于该方法调用内。
class Foo {
void doSomething() {
int x = 0; // x inside doSomething
JButton btn = new Button("Do Something");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
/*
* This does not happen now:
* it happens later.
*
* By the time this happens
* the method will have exited
* and x will be gone.
*
*/
}
});
/* That x disappears forever here.
* (Or a little bit later but basically here.)
*/
}
}
You can declare x as final and then the anonymous class is given the value of it but you are wanting to change it. 您可以将x声明为final,然后为匿名类提供它的值,但您想更改它。 Probably what you are wanting is to make it a field: 可能您想要将其设置为一个字段:
class Foo {
int x = 0; // x inside Foo
JButton btn = new Button("Do Something");
Foo() {
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
/* do something with x as needed */
}
});
}
}
Anonymous classes can also have fields: 匿名类也可以具有字段:
btn.addActionListener(new ActionListener() {
int x = 0; // x inside anonymous ActionListener
@Override
public void actionPerformed(ActionEvent ae) {
/* use x only in actionPerformed */
}
});
"I have also tried declaring x as a member of foo() but that results to x being 0 or null because for some reason once it exits the button click method it takes x to back to its old value: 0 or null." “我也曾尝试将x声明为foo()的成员,但结果导致x为0或null,因为由于某种原因,一旦退出按钮click方法,它将x返回其旧值:0或null。”
Unfortunately, this probably means you're doing something else wrong. 不幸的是,这可能意味着您在做其他错误的事情。 I'm not sure what that might be from your description and code. 我不确定您的描述和代码可能是什么。 For example, you're creating a new JButton locally inside a method which seems unusual. 例如,您正在一个方法内部本地创建一个新的JButton,这似乎很不正常。 Unless it's one of those "createAndShowGUI" methods (as seen all over the Swing tutorials ), you might be creating multiple buttons, multiple listeners, etc. 除非它是那些“ createAndShowGUI”方法之一(如在Swing教程中看到的那样),否则您可能正在创建多个按钮,多个侦听器等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.