Java 如何从超类中调用子类方法?
How do you call a subclass method from a superclass in Java?
我四处寻找我的问题的答案,但我找不到。如何从 Java 中的 superclass 调用 subclass 方法?
基本上我想要做的是:我有一个名为 exec
的方法,它将 String
作为命令的参数。我希望能够调用开发人员从 superclass 覆盖的 subclass 中的 exec
方法,而无需提前知道 subclasses 名称。
这就像 Thread
class 的工作方式。我不想做我发现的每个答案所做的事情,即 Superclass object = new Subclass();
然后只需调用 object.method();
.
这是super中的代码class
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.text.Text;
public abstract class Console extends Application {
private String title;
private static Text output = new Text();
public void create(String title) {
this.title = title;
launch();
}
public void start(Stage stage) {
stage.setOnCloseRequest((WindowEvent event) -> {
System.exit(0);
});
stage.setTitle(title);
stage.setResizable(false);
Group root = new Group();
Scene scene = new Scene(root, 800, 400);
stage.setScene(scene);
ScrollPane scroll = new ScrollPane();
scroll.setContent(output);
scroll.setMaxWidth(800);
scroll.setMaxHeight(360);
TextField input = new TextField();
input.setLayoutX(0);
input.setLayoutY(380);
input.setPrefWidth(800);
scene.setOnKeyPressed((KeyEvent event) -> {
if(event.getCode() == KeyCode.ENTER) {
exec(input.getText());
input.clear();
}
});
root.getChildren().add(scroll);
root.getChildren().add(input);
stage.show();
}
public static void appendOutput(String value) {
Platform.runLater(() -> {
output.setText(output.getText() + "\n" + value);
});
}
protected abstract void exec(String command);
}
[...] I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time.
您不需要做任何特别的事情。您只需从基础 class 的代码中调用它即可。 Java 中的所有方法都是虚拟的,因此将调用 subclass 的重写实现。
这是一个演示:
public class Test {
public static void main(String[] args) {
Console console = new ConsoleSubclass();
console.start();
console.keyPressed();
}
}
abstract class Console {
Runnable keyPressedHandler;
private void setOnKeyPressed(Runnable handler) {
keyPressedHandler = handler;
}
public void keyPressed() {
keyPressedHandler.run();
}
public void start() {
setOnKeyPressed(() -> {
exec();
});
}
abstract void exec();
}
class ConsoleSubclass extends Console {
@Override
void exec() {
System.out.println("In ConsoleSubclass");
}
}
public class UsingClass
{
SuperClassType x = null;
x = goGetClassReference(); // this can get an instance of SuperClassType,
// or of any subclass of it.
x.methodName(); // this calls methodName on the class obtained above;
//if goGetClassReference got a subclass, this calls methodName() on the subclass.
}
我的特定 JavaFX 子类化问题的答案在此处 。对于一般的子类化,这里的答案已经足够了。
我四处寻找我的问题的答案,但我找不到。如何从 Java 中的 superclass 调用 subclass 方法?
基本上我想要做的是:我有一个名为 exec
的方法,它将 String
作为命令的参数。我希望能够调用开发人员从 superclass 覆盖的 subclass 中的 exec
方法,而无需提前知道 subclasses 名称。
这就像 Thread
class 的工作方式。我不想做我发现的每个答案所做的事情,即 Superclass object = new Subclass();
然后只需调用 object.method();
.
这是super中的代码class
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.text.Text;
public abstract class Console extends Application {
private String title;
private static Text output = new Text();
public void create(String title) {
this.title = title;
launch();
}
public void start(Stage stage) {
stage.setOnCloseRequest((WindowEvent event) -> {
System.exit(0);
});
stage.setTitle(title);
stage.setResizable(false);
Group root = new Group();
Scene scene = new Scene(root, 800, 400);
stage.setScene(scene);
ScrollPane scroll = new ScrollPane();
scroll.setContent(output);
scroll.setMaxWidth(800);
scroll.setMaxHeight(360);
TextField input = new TextField();
input.setLayoutX(0);
input.setLayoutY(380);
input.setPrefWidth(800);
scene.setOnKeyPressed((KeyEvent event) -> {
if(event.getCode() == KeyCode.ENTER) {
exec(input.getText());
input.clear();
}
});
root.getChildren().add(scroll);
root.getChildren().add(input);
stage.show();
}
public static void appendOutput(String value) {
Platform.runLater(() -> {
output.setText(output.getText() + "\n" + value);
});
}
protected abstract void exec(String command);
}
[...] I have a method called exec that takes a String as a parameter for a command. I want to be able to call the exec method in the subclass that the developer has overridden from the superclass without knowing the subclasses name ahead of time.
您不需要做任何特别的事情。您只需从基础 class 的代码中调用它即可。 Java 中的所有方法都是虚拟的,因此将调用 subclass 的重写实现。
这是一个演示:
public class Test {
public static void main(String[] args) {
Console console = new ConsoleSubclass();
console.start();
console.keyPressed();
}
}
abstract class Console {
Runnable keyPressedHandler;
private void setOnKeyPressed(Runnable handler) {
keyPressedHandler = handler;
}
public void keyPressed() {
keyPressedHandler.run();
}
public void start() {
setOnKeyPressed(() -> {
exec();
});
}
abstract void exec();
}
class ConsoleSubclass extends Console {
@Override
void exec() {
System.out.println("In ConsoleSubclass");
}
}
public class UsingClass
{
SuperClassType x = null;
x = goGetClassReference(); // this can get an instance of SuperClassType,
// or of any subclass of it.
x.methodName(); // this calls methodName on the class obtained above;
//if goGetClassReference got a subclass, this calls methodName() on the subclass.
}
我的特定 JavaFX 子类化问题的答案在此处