Java:使用局部变量的匿名内部类
如何在我的匿名内部子类中获取传递给此方法的 userId
值?
public void doStuff(String userID) {
doOtherStuff(userID, new SuccessDelegate() {
@Override
public void onSuccess() {
Log.e(TAG, "Called delegate!!!! "+ userID);
}
});
}
我收到此错误:
无法引用不同方法中定义的内部类中的非最终变量 userID
我很确定我不能将其分配为最终变量,因为它是一个具有未知值的变量。我听说这种语法确实以某种方式保留了范围,所以我认为一定有一个我不太了解的语法技巧。
How can I get the value of userId
passed to this method in my anonymous inner subclass here?
public void doStuff(String userID) {
doOtherStuff(userID, new SuccessDelegate() {
@Override
public void onSuccess() {
Log.e(TAG, "Called delegate!!!! "+ userID);
}
});
}
I get this error:
Cannot refer to a non-final variable userID inside an inner class defined in a different method
I'm pretty sure I can't assign it as final since it's a variable with an unknown value. I had heard that this syntax does preserve scope in some way, so I think there must be a syntax trick I don't quite know yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如这里其他人所说,局部变量必须是最终的才能被内部类访问。
这(基本上)是为什么......如果您编写以下代码(很长的答案,但是在底部,您可以获得简短的版本:-):
编译器大致如下翻译:
然后是:
和最后:
重要的一点是将构造函数添加到 $1 的位置。想象一下,如果您可以这样做:
您会期望 foo.bar() 会打印出 1,但实际上会打印出 42。通过要求局部变量为最终变量,就不会出现这种令人困惑的情况。
As everyone else here has said, local variables have to be final to be accessed by an inner class.
Here is (basically) why that is... if you write the following code (long answer, but, at the bottom, you can get the short version :-):
the compiler translates it roughly like this:
and then this:
and finally to this:
The important one is where it adds the constructor to $1. Imagine if you could do this:
You would expect that foo.bar() would print out 1 but it would actually print out 42. By requiring local variables to be final this confusing situation cannot arise.
当然你可以将它指定为最终的 - 只需将该关键字放在参数的声明中:
我不确定你的意思是它是一个具有未知值的变量;最终的意思是,一旦将值分配给变量,就不能重新分配它。由于您没有在方法中更改 userID 的值,因此在这种情况下将其确定为最终值是没有问题的。
Sure you can assign it as final - just put that keyword in the declaration of the parameter:
I'm not sure what you meant about it being a variable with an unknown value; all that final means is that once a value is assigned to the variable, it cannot be re-assigned. Since you're not changing the value of the userID within your method, there's no problem making it final in this case.
在 Java 8 中,这种情况发生了一些变化。您现在可以访问实际上是最终的变量。相关片段和示例来自 Oracle 文档(强调我的):
有效最终: 非最终变量或参数,其值在初始化后从未改变,因此实际上是最终的。
In Java 8, this has changed a little bit. You can now access variables that are effectively final. Relevant snippet and example from the Oracle documentation (emphasis mine):
Effectively final: A non-final variable or parameter whose value is never changed after it is initialized is effectively final.
将其设为
final
有什么问题吗?What's the problem with making it
final
as in声明方法
值需要是最终的,以便编译器可以确保它不会改变。这意味着编译器可以随时将值绑定到内部类,而不必担心更新。
该值在您的代码中没有更改,因此这是一个安全的更改。
declare the method
The value needs to be final so that the compiler can be sure it doesn't change. This means the compiler can bind the value to the inner class at any time, without worrying about updates.
The value isn't changing in your code so this is a safe change.