我想使我的超类成为抽象类,但是尝试这样做时会遇到各种异常(InstantiationException,RuntimeException,InvocationTargetException)。当我没有超类作为抽象时,我的代码将起作用。我该如何解决?
我已经尝试将我的“ main”和“ start”函数包括在抽象类主体之外,因为我认为在抽象类内部调用这些函数可能是一个问题。我将这些方法移到扩展抽象类的子类中。然后,我覆盖了每个子类中的抽象方法“ start”。重新格式化并没有解决任何问题,因为我遇到了相同的错误。根据我对这个问题How do you call a subclass method from a superclass in Java?的研究,该用户使用的格式接近于我的抽象超类扩展“应用程序”所试图实现的格式。
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); public 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); } } 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(); } }
[当我没有超类作为抽象类并且draw不再是抽象方法时,我得到了它,这是我想要的输出:https://i.imgur.com/J4XB8bo.png
但是,否则我收到以下错误消息:
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{ . . . }
我还再次尝试将内部类“ MyLine”从超类“ 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); } }
Laslty我设计了一个新的类“ Main”来扩展“ Test”并从那里启动应用程序,如下所示:
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”中引发了新的异常:
Exception in thread "main" java.lang.reflect.InvocationTargetException
我想使我的超类成为抽象类,但是尝试这样做时会遇到各种异常(InstantiationException,RuntimeException,InvocationTargetException)。当我...
如果超类是抽象的,则无法实例化子类的原因是,子类是超类的inner
我通过将我的类文件重命名为“ Main”找到了可行的解决方案。这使我得以保留抽象类“ Test”及其所有子类,并通过扩展了“应用程序”的新公共类“ Main”启动了应用程序。