我是使用RxVertx(一种类似于RxJava的技术)和Java8进行开发,但出现了编译错误。
这是我的代码:
public rx.Observable<Game> findGame(long templateId, GameModelType game_model, GameStateType state) {
return context.findGame(templateId, state)
.flatMap(new Func1<RxMessage<byte[]>, rx.Observable<Game>>() {
@Override
public Observable<Game> call(RxMessage<byte[]> gameRawReply) {
Game game = null;
switch(game_model) {
case SINGLE: {
ebs.subscribe(new Action1<RxMessage<byte[]>>() {
@Override
public void call(RxMessage<byte[]> t1) {
if(!singleGame.contains(0) {
game = new Game(); // ERROR is at this line
singleGames.put(0, game);
} else {
game = singleGames.get(0); // ERROR is at this line
}
}
});
}
}
return rx.Observable.from(game);
}
});
}
编译错误为:“定义在封闭作用域中的局部变量game必须是final或有效final”。由于我在函数末尾进行了分配、设置并返回'game',所以我无法将其定义为final。如何使这段代码编译通过?谢谢。
public void call
会立即执行吗?我不确定这段代码是否按照你的预期运行。 - Sebasgame = singleGames.get(0)
在rx.Observable.from(game)
之前执行吗?看起来似乎不是这样。 - talexAtomicReference
,但是其他人已经指出,在这种情况下使用它是可疑的。 - Stuart Marks