如何从Java中的超类中调用子类方法?

时间:2015-01-19 19:50:23

标签: java inheritance subclass superclass

我在这里环顾四周找到了我的问题的答案而我不能。如何从Java中的超类中调用子类方法?

基本上我要做的是:我有一个名为exec的方法,它将String作为命令的参数。我希望能够在子类中调用开发人员从超类重写的子类中的exec方法,而不需要提前知道子类名称。

这就像Thread类的工作方式一样。我不打算做我发现的每个答案Superclass object = new Subclass();然后只调用object.method();

这是超类中的代码

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);
}

3 个答案:

答案 0 :(得分:4)

  

[...]我有一个名为exec的方法,它将String作为命令的参数。我希望能够在开发人员从超类中重写的子类中调用exec方法,而不必提前知道子类名称。

你不需要做任何特别的事情。您只需从基类的代码中调用它。 Java中的所有方法都是虚拟的,因此将调用子类的重写实现。

这是一个演示:

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");
    }
}

答案 1 :(得分:0)

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.
}

答案 2 :(得分:0)

我特定的JavaFX子类化问题的答案就在这里Subclassing a JavaFX Application。对于一般的子类化,这里的答案非常充分。

相关问题