![](/img/trans.png)
[英]How do i access the non-final variable number inside an inner class in Java?
[英]How do you make a "counter" count in Java if I can't access a non-final variable from the inner class?
我正在為我的課程做一個小的編碼練習,但我似乎無法克服我遇到的問題。 我想在每次有人單擊按鈕時進行計數,但我無法創建變量來存儲信息,因為我必須在事件處理程序外部創建變量,這樣每次按下按鈕時它都不會重置為 0。
但是,每當我在事件處理程序外部創建一個變量並嘗試將其加 1 時,它就會說無法從內部 class 訪問它。
public class HelloApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Counter Application");
Label label = new Label("Counter");
Button btn = new Button();
btn.setText("Count");
int count = 0;
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
count++;
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
您可以在事件處理程序中將其設為實例變量:
public class HelloApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Counter Application");
Label label = new Label("Counter");
Button btn = new Button();
btn.setText("Count");
btn.setOnAction(new EventHandler<ActionEvent>() {
private int count = 0 ;
@Override
public void handle(ActionEvent event) {
count++;
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
或者你可以讓計數器成為 class 周圍的一個實例變量:
public class HelloApplication extends Application {
public static void main(String[] args) {
launch(args);
}
private int count = 0 ;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Counter Application");
Label label = new Label("Counter");
Button btn = new Button();
btn.setText("Count");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
count++;
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
或者您可以定義一個包含可變 int 的包裝器 class:
public class HelloApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Counter Application");
Label label = new Label("Counter");
Button btn = new Button();
btn.setText("Count");
class IntWrapper {
private int x = 0 ;
public int getX() {
return x;
}
public void increment() {
x++;
}
}
IntWrapper count = new IntWrapper();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
count.increment();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
最后兩個選項讓您將事件處理程序實現為 lambda 表達式
btn.setOnAction(event -> count.increment());
而第一個不允許你這樣做。
count
class HelloApplication
的成員並使用方法引用。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloApplication extends Application {
private int count;
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Counter Application");
Label label = new Label("Counter");
Button btn = new Button();
btn.setText("Count");
btn.setOnAction(this::buttonClickCounter);
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private void buttonClickCounter(ActionEvent event) {
count++;
}
}
如您所見,您可以為該方法命名任何您喜歡的名稱,並給它任何您喜歡的訪問修飾符。 只需確保它返回相同的值並接受與接口EventHandler
中的方法handle
相同的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.