变量的范围 [英] scope of a variable
本文介绍了变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能得到这code的方法参数myInteger的值。
公共无效myMethod的(INT myInteger){
View.OnClickListener myClearHandler =新View.OnClickListener(){
公共无效的onClick(视图v){
// ***我怎样才能在这里myInteger的价值?***
}
};
}
解决方案
假设你只是想的读的话,你只需要让它决赛:
公共无效myMethod的(最终诠释myInteger){
View.OnClickListener myClearHandler =新View.OnClickListener(){
公共无效的onClick(视图v){
INT富= myInteger;
}
};
}
在Java中,只有最后
参数和局部变量可以匿名内部类中访问。
How can I get the value of a method parameter "myInteger" in this code.
public void myMethod(int myInteger) {
View.OnClickListener myClearHandler = new View.OnClickListener() {
public void onClick(View v) {
//***How can I get the value of "myInteger" here?***
}
};
}
解决方案
Assuming you're just trying to read it, you just need to make it final:
public void myMethod(final int myInteger) {
View.OnClickListener myClearHandler = new View.OnClickListener() {
public void onClick(View v) {
int foo = myInteger;
}
};
}
In Java, only final
parameters and local variables can be accessed within anonymous inner classes.
这篇关于变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文