我使用了一个局部变量来存储this
,以便使其可以在内联覆盖中访问,但对此并不满意。有没有更优雅的方式来做到这一点?
public class Tree extends Plant {
public Tree() {
timeline = new Timeline(new KeyFrame(Duration.millis(40), new EventHandler<ActionEvent>() {
final Plant tree = this; // <---- here
@Override
public void handle(ActionEvent actionEvent) {
tree.setA(5); // <---- and here
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
}
}