Java 如何在不获取实例化异常的情况下将这个超类更改为抽象类?

Java 如何在不获取实例化异常的情况下将这个超类更改为抽象类?,java,javafx,abstract-class,Java,Javafx,Abstract Class,我想使我的超类成为一个抽象类,但是在尝试这样做时,我会遇到各种异常(InstanceionException、RuntimeException、InvocationTargetException)。当我没有将超类作为抽象类时,我的代码可以工作。我该如何解决这个问题 我曾尝试将我的“main”和“start”函数包含在抽象类主体之外,因为我认为在抽象类中调用这些函数可能会有问题。我将这些方法移动到扩展抽象类的子类中。然后在每个子类中重写抽象方法“start”。尽管如此,这种重新格式化并没有解决任何

我想使我的超类成为一个抽象类,但是在尝试这样做时,我会遇到各种异常(InstanceionException、RuntimeException、InvocationTargetException)。当我没有将超类作为抽象类时,我的代码可以工作。我该如何解决这个问题

我曾尝试将我的“main”和“start”函数包含在抽象类主体之外,因为我认为在抽象类中调用这些函数可能会有问题。我将这些方法移动到扩展抽象类的子类中。然后在每个子类中重写抽象方法“start”。尽管如此,这种重新格式化并没有解决任何问题,因为我得到了相同的错误。从我在这个问题中所研究的内容来看,这个用户使用的格式接近于我试图通过扩展抽象超类“应用程序”来实现的格式

当我没有将我的超类作为抽象类,并且draw不再是一个抽象方法时,我得到了我想要的输出:

但是,否则我将收到以下错误消息:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class tutorial.application.Test
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.InstantiationException
    at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application tutorial.application.Test
编辑: 到目前为止,我已经尝试了3个明显不同的选项,但都出现了错误。首先,我尝试在层次结构中使子类保持静态,如下所示:

static class MyLine extends Test{
.
.
.
}
public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);

    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }
}
package tutorial.application;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import java.lang.Math;


public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);


}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }

    @Override
    public void start(Stage arg0) throws Exception {

    }
}

class Main extends Test{
    private Main() {
        super(0,0);
    }
    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    @Override
    public void draw(GraphicsContext GC) {

    }
}
我还尝试将内部类“MyLine”从超类“Test”中去掉,如下所示:

static class MyLine extends Test{
.
.
.
}
public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);

    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }
}
package tutorial.application;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import java.lang.Math;


public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);


}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }

    @Override
    public void start(Stage arg0) throws Exception {

    }
}

class Main extends Test{
    private Main() {
        super(0,0);
    }
    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    @Override
    public void draw(GraphicsContext GC) {

    }
}
Laslty I设计了一个新类“Main”,以扩展“Test”,并从中启动应用程序,如下所示:

static class MyLine extends Test{
.
.
.
}
public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);

    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }
}
package tutorial.application;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import java.lang.Math;


public abstract class Test extends Application{

    private int width = 0;
    private int height = 0;

    public Test() {
        this.width = 0;
        this.height = 0;
    }

    public Test(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public abstract void draw(GraphicsContext GC);


}

class MyLine extends Test{

    private int x1, x2, y1, y2;

    public MyLine(int x1, int y1, int x2, int y2) {
        super(0, 0);
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    @Override
    public void draw(GraphicsContext GC){
        GC.strokeLine(x1, y1, x2, y2);
    }

    @Override
    public void start(Stage arg0) throws Exception {

    }
}

class Main extends Test{
    private Main() {
        super(0,0);
    }
    public static void main(String []args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        int canvasx = 400, canvasy = 650;
        MyLine line = new MyLine(0, 0, canvasx, canvasy);
        MyLine line2 = new MyLine(0, canvasy, canvasx, 0);
        Group root = new Group();
        Canvas canvas = new Canvas(canvasx, canvasy);
        GraphicsContext GC = canvas.getGraphicsContext2D();
        line.draw(GC);
        line2.draw(GC);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    @Override
    public void draw(GraphicsContext GC) {

    }
}
前两个仍然会导致相同的错误,我认为最后一个会在线程“main”中引发新的异常,从而使问题更加严重:


如果超类是抽象的,则不能实例化子类的原因是子类是超类的内部类,要实例化内部类,需要包含类的实例,但根据定义,不能实例化抽象类


将子类设置为静态,你的问题就会得到解决。

我通过将类文件重命名为“Main”找到了一个有效的解决方案。这使我能够保留抽象类“Test”及其所有子类,并通过扩展“Application”的新公共类“Main”启动了应用程序

如果您阅读javadoc中的
实例化异常
,您将看到抽象类无法通过反射实例化。如果研究发布的堆栈跟踪,您将看到代码试图通过反射实例化
测试
类。
launch
始终启动包含调用
launch
(或类似)的方法的类,这就是
测试
类。您可以指定不同的类作为
Launch
方法的附加参数。请注意,
MyLine
也不起作用,因为它是一个非
静态的内部类,因此需要一个包含类的参数来构造。(可以反射调用的构造函数不需要传递任何参数。)是否有理由MyLine会成为它继承自的抽象的内部类?我的意思是,即使这样做有效,那有什么意义呢?@MichelFeldheim它只有在引用超类中的字段时才有意义。