在Java中访问外部类的变量 [英] access variables of outer class in Java
本文介绍了在Java中访问外部类的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
//在这里我想做一些**对话框**变量
.......
}
});
解决方案
如果对话框变量是外部类的字段,你可以使用这个
作为外部类名前缀(一个合格的):
send.setOnClickListener(new View.OnClickListener ()
{
public void onClick(View v){
ProgressDialog dlg = OuterClass.this.dialog;
.......
}
});
或者,如果dialiog变量是局部变量,则需要将其标记为final:
final ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
//对话框变量是在这里的范围...
dialog.someMethod();
}
});
in Java android application how can i access variables of outer class from the inner anonymous class ? Example:
ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//here i'd like to do something with **dialog** variable
.......
}
});
解决方案
If the dialog variable is a field of the outer class, you can use this
prefixed with the outer class name (a qualified this):
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
ProgressDialog dlg = OuterClass.this.dialog;
.......
}
});
Alternatively, if the dialiog variable is a local variable it needs to be marked as final:
final ProgressDialog dialog = new ProgressDialog(this);
.....
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// The dialog variable is in scope here ...
dialog.someMethod();
}
});
这篇关于在Java中访问外部类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文