如何在没有对象的情况下访问非静态方法?
How is a non-static method accessed without an object?
我是 JavaFX 的新手。在我下面的代码中,getHBox()
是一个非静态方法,无需创建对象即可访问。
public class Main extends Application {
public void start(Stage primaryStage) {
//Main m = new Main();
try {
BorderPane rootPane = new BorderPane();
rootPane.setTop(getHBox());
//rootPane.setTop(m.getHBox());
Scene scene = new Scene(rootPane,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public HBox getHBox()
{
HBox hb = new HBox(15);
hb.getChildren().add(new Button("Press"));
return hb;
}
public static void main(String[] args) {
launch(args);
}
}
现在我已经查看了 Whosebug 中的答案。伙计们正在谈论 class 成员。 getHBox()
方法与其他方法有何不同?请提供一些解释或指导我阅读适当的教程。
In my following code, getHBox()
is a non-static method is accessed without creating an object.
这是不正确的。如所提供的代码中所用,getHBox()
仅由另一个 non-static 方法 start()
调用。作为实例方法本身,start()
必须在一个对象上调用(例如,由 JavaFX 实例化的对象)。在不指定目标对象的情况下调用 getHBox()
将隐式定向到同一对象,就好像它是 this.getHBox()
.
How is getHBox()
method different from any other method?
它不是,在任何相关意义上都不是,也不是特定于 JavaFX 的。
我是 JavaFX 的新手。在我下面的代码中,getHBox()
是一个非静态方法,无需创建对象即可访问。
public class Main extends Application {
public void start(Stage primaryStage) {
//Main m = new Main();
try {
BorderPane rootPane = new BorderPane();
rootPane.setTop(getHBox());
//rootPane.setTop(m.getHBox());
Scene scene = new Scene(rootPane,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public HBox getHBox()
{
HBox hb = new HBox(15);
hb.getChildren().add(new Button("Press"));
return hb;
}
public static void main(String[] args) {
launch(args);
}
}
现在我已经查看了 Whosebug 中的答案。伙计们正在谈论 class 成员。 getHBox()
方法与其他方法有何不同?请提供一些解释或指导我阅读适当的教程。
In my following code,
getHBox()
is a non-static method is accessed without creating an object.
这是不正确的。如所提供的代码中所用,getHBox()
仅由另一个 non-static 方法 start()
调用。作为实例方法本身,start()
必须在一个对象上调用(例如,由 JavaFX 实例化的对象)。在不指定目标对象的情况下调用 getHBox()
将隐式定向到同一对象,就好像它是 this.getHBox()
.
How is
getHBox()
method different from any other method?
它不是,在任何相关意义上都不是,也不是特定于 JavaFX 的。