是否可以传递参数或访问匿名类的外部参数?例如:
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
}
});
有没有办法让监听器访问到我的变量或者在不将监听器创建为实际命名类的情况下将变量传递给它?
是否可以传递参数或访问匿名类的外部参数?例如:
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
}
});
有没有办法让监听器访问到我的变量或者在不将监听器创建为实际命名类的情况下将变量传递给它?
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
private int anonVar;
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
// It's now here:
System.out.println("Initialized with value: " + anonVar);
}
private ActionListener init(int var){
anonVar = var;
return this;
}
}.init(myVariable) );
不需要 'final' 声明。
init()
函数必须返回this
?我不太理解这个语法。 - Jori从技术上讲,不行,因为匿名类不能有构造函数。
但是,类可以引用包含作用域的变量。对于匿名类,这些可以是包含类的实例变量或标记为final的局部变量。
编辑:正如Peter所指出的,您还可以向匿名类的超类构造函数传递参数。
new ArrayList(10) { }
。 - Peter Lawreysuper()
之后和实际构造函数的其余部分之前执行。语法为:new someclass(){ fields; {initializer} fields; methods(){} }
。这有点像静态初始化程序,但没有 static 关键字。具体可见 Java 语言规范第 8.6 节。 - Mark Jeronimus是的,你可以捕获变量并将其暴露给内部类。唯一的限制是它必须是final。
this
来引用,而 this
是一个常量。 - Peter Lawrey就像这样:
final int myVariable = 1;
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Now you can access it alright.
}
});
这将会产生神奇的效果
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
int myVariable;
public void actionPerformed(ActionEvent e) {
// myVariable ...
}
public ActionListener setParams(int myVariable) {
this.myVariable = myVariable;
return this;
}
}.setParams(myVariable));
final
变量一起使用,正如aav所述,但是这个答案没有提供这方面的信息。到目前为止,最好的答案是由Adam Mlodzinksi给出的(我现在专门使用这种模式,不再使用final!)。我坚持认为这没有回答所问的问题。 - Matt Klein我的解决方案是使用一个返回实现的匿名类的方法。常规参数可以传递给此方法,并在匿名类中使用。
例如:(用于处理文本框更改的一些GWT代码):
/* Regular method. Returns the required interface/abstract/class
Arguments are defined as final */
private ChangeHandler newNameChangeHandler(final String axisId, final Logger logger) {
// Return a new anonymous class
return new ChangeHandler() {
public void onChange(ChangeEvent event) {
// Access method scope variables
logger.fine(axisId)
}
};
}
在这个例子中,新的匿名类方法将被引用为:
textBox.addChangeHandler(newNameChangeHandler(myAxisName, myLogger))
或者,根据原帖的要求:private ActionListener newActionListener(final int aVariable) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Your variable is: " + aVariable);
}
};
}
...
int myVariable = 1;
newActionListener(myVariable);
myVariable
成为final,您必须将其包装在一个新的作用域中,在这个作用域中它是否是final并不重要。int myVariable = 1;
{
final int anonVar = myVariable;
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
// Use anonVar instead of myVariable
}
});
}
init
方法。我可以添加一行额外的代码到我的例子中,以便拥有这个额外的变量。如果你是建造者模式的铁杆粉丝,请随意使用它们,但在这种情况下并不需要。 - cevingint myVariable = 1;
ActionListener al = ae->System.out.println(myVariable);
myButton.addActionListener( al );
Function<Integer,ActionListener> printInt =
intvar -> ae -> System.out.println(intvar);
int myVariable = 1;
myButton.addActionListener( printInt.apply(myVariable) );
public class Foo {
int myVariable = 1;
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Foo.this.myVariable = 8;
}
});
}