从内部类访问变量
我有一些代码为回调处理程序定义了一个匿名内部类。该处理程序需要分配一个局部变量,请参见下文。我需要在回调中分配 resp
并在函数末尾引用它。但是,我在 Eclipse 中收到此错误:
无法分配最终局部变量 resp
,因为它是在封闭类型中定义的
我该如何解决此问题?
DoorResult unlockDoor(final LockableDoor door) {
final UnlockDoorResponse resp;
final boolean sent = sendRequest(new UnlockDoorRequest(door),
new ResponseAction() {
public void execute(Session session)
throws TimedOutException, RetryException, RecoverException {
session.watch(UNLOCK_DOOR);
resp = (UnlockDoorResponse)session.watch(UNLOCK_DOOR);
}
});
DoorResult result;
if (!sent) {
return DoorResult.COMMS_ERROR;
}
else {
return DoorResult.valueOf(resp.getResponseCode());
}
}
I've got some code which defines an anonymous inner class for a callback handler. This handler needs to assign a local variable, see below. I need to assign resp
in the callback and refer to it towards the end of the function. I am getting this error in Eclipse however:
The final local variable resp
cannot be assigned, since it is defined in an enclosing type
How can I fix this?
DoorResult unlockDoor(final LockableDoor door) {
final UnlockDoorResponse resp;
final boolean sent = sendRequest(new UnlockDoorRequest(door),
new ResponseAction() {
public void execute(Session session)
throws TimedOutException, RetryException, RecoverException {
session.watch(UNLOCK_DOOR);
resp = (UnlockDoorResponse)session.watch(UNLOCK_DOOR);
}
});
DoorResult result;
if (!sent) {
return DoorResult.COMMS_ERROR;
}
else {
return DoorResult.valueOf(resp.getResponseCode());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个适用于您的情况的技巧:
但是,如果您想要一个更清晰的解决方案,则必须为处理程序定义一个命名类,将响应存储在其字段中,并使用访问器方法检索它。
此致,
斯坦.
Here is a hack that would work in your case:
If you want a cleaner solution, though, you have to define a named class for your handler, store the response in its field, and retrieve it using an accessor method.
Best regards,
Stan.
您可以通过为响应创建一个包装类来解决这个问题。
然后,您的代码将如下所示:
You could get around this by creating a wrapper class for the response.
Then, your code would look like:
假设这是您要更改的代码,如何更改
sendRequest
和ResponseAction.execute
以返回UnlockDoorResponse
的实例Assuming this is your code to change, how about changing
sendRequest
andResponseAction.execute
to return an instance ofUnlockDoorResponse
如果要返回结果,请使用命名内部类而不是匿名内部类。提供的所有其他选项都是恕我直言的丑陋黑客(一个自我承认的;-)
(好吧,@Joel 不是,但假设您可以更改正在实现的接口)
只需创建一个带有结果 getter 的类实例,它就是干净,只需要您实现单个类。
If you are going to return results, then use a named inner class instead of an anonymous one. All the other options presented are IMHO ugly hacks (one self admitted ;-)
(OK, @Joel's is not but assumes you can change the interface you are implementing)
Just create an instance of the class with a getter for the result, it is clean and only requires you to implement the single class.