如何在我的匿名内部子类中获取传递给此方法的 userId
的值?
public void doStuff(String userID) {
doOtherStuff(userID, new SuccessDelegate() {
@Override
public void onSuccess() {
Log.e(TAG, "Called delegate!!!! "+ userID);
}
});
}
我收到这个错误:
Cannot refer to a non-final variable userID inside an inner class defined in a different method
我很确定我不能将它指定为最终变量,因为它是一个具有未知值的变量。我听说这种语法确实以某种方式保留了范围,所以我认为一定有一个我还不太了解的语法技巧。
最佳答案
正如这里的其他人所说,局部变量必须是最终的才能被内部类访问。
这(基本上)是为什么...如果您编写以下代码(答案很长,但在底部,您可以获得简短版本:-):
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
foo = new Foo()
{
public void bar()
{
System.out.println(x);
}
};
foo.bar();
}
}
编译器大致是这样翻译的:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
class $1
implements Foo
{
public void bar()
{
System.out.println(x);
}
}
foo = new $1();
foo.bar();
}
}
然后是:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
foo = new $1(x);
foo.bar();
}
private static class $1
implements Foo
{
private final int x;
$1(int val)
{
x = val;
}
public void bar()
{
System.out.println(x);
}
}
}
最后是这个:
class Main
{
public static void main(String[] args)
{
final int x;
Main$Foo foo;
x = 42;
foo = new Main$1(x);
foo.bar();
}
}
interface Main$Foo
{
void bar();
}
class Main$1
implements Main$Foo
{
private final int x;
Main$1(int val)
{
x = val;
}
public void bar()
{
System.out.println(x);
}
}
重要的是它将构造函数添加到 $1 的位置。想象一下,如果您可以这样做:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
int x;
Foo foo;
x = 42;
foo = new Foo()
{
public void bar()
{
System.out.println(x);
}
};
x = 1;
foo.bar();
}
}
您可能期望 foo.bar() 会打印出 1,但它实际上会打印出 42。通过要求局部变量为 final,就不会出现这种令人困惑的情况。
关于Java:使用局部变量的匿名内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3251018/